Ruby on rails Rails,从数组中删除字符串引号,而不将其转换为字符串

Ruby on rails Rails,从数组中删除字符串引号,而不将其转换为字符串,ruby-on-rails,arrays,Ruby On Rails,Arrays,我有一个方法,它将数组作为参数,例如: a = ["title", "item"] 我需要摆脱“,但我很难做到这一点 我的目标是实现以下目标: a = [title, item] 这里提出了两种可能的解决办法: 及 我尝试了这两种解决方案,但总是导致以下错误: undefined local variable or method `title' 如何去掉数组中的这些“引号” 编辑: 我需要改变一个数组。这就是我想做的: a = ["title", "item"] 应该改为: a = [m

我有一个方法,它将数组作为参数,例如:

a = ["title", "item"]
我需要摆脱
,但我很难做到这一点

我的目标是实现以下目标:

a = [title, item]
这里提出了两种可能的解决办法:

我尝试了这两种解决方案,但总是导致以下错误:

undefined local variable or method `title'
如何去掉数组中的这些“引号”

编辑:

我需要改变一个数组。这就是我想做的:

a = ["title", "item"]
应该改为:

a = [model_class.human_attribute_name(:title), model_class.human_attribute_name(:title)]
(关于翻译)

此代码位于model.rb中,可能会有所帮助。以下是我的完整代码:

def humanifier(to_translate_array)
  translated = []

  to_translate_array.each do |element|
    translated.push("model_class.human_attribute_name(:#{element})")
  end


  return translated
end

看起来你想把字符串翻译成符号,你可以用
#to_sym

  to_translate_array.each do |element|
    translated.push("model_class.human_attribute_name(#{element.to_sym})")
  end
或者,如果您确实需要转换后的值,(而不仅仅是字符串“model_class.human…”)


“title”
是一个字符串,
:title
是一个符号。

为什么要去掉它?是否需要特定的数据类型?除非已将
dog
定义为变量,否则无法将
“dog”
转换为
dog
。你可以叫
“dog.intern
,但那会给你符号
:dog
,你必须去掉冒号。它只带引号显示,因为它告诉您它是一个字符串,当您实际打印出来时,它不会带引号。请定义您的用例,以便我们更好地回答您的问题。@jeff,编辑了我的问题谢谢!正是我需要的一个旁白,
#intern
#to#sym
:)的别名@Jeff说得好!取决于您的概念模型。。。我是否正在将字符串更改为符号?。。。我是否正在将字符串更改为其内部表示形式?这可能是OP在他的世界观中会更好地理解
#intern
def humanifier(to_translate_array)
  translated = []

  to_translate_array.each do |element|
    translated.push("model_class.human_attribute_name(:#{element})")
  end


  return translated
end
  to_translate_array.each do |element|
    translated.push("model_class.human_attribute_name(#{element.to_sym})")
  end
  to_translate_array.each do |element|
    translated.push(model_class.human_attribute_name(element.to_sym))
  end