我的元素可以是红色、黄色、绿色(比这更复杂)等几个属性之一,并且它们具有相应的 is_red、is_yellow、is_green bool 值。
我想要一个 api 函数来更改我传递的这个值,例如:
id: 12
type: red
在我的函数中有:
item=Item.find(id)
if item.is_ + type == true
item.is_ + type=false
end
我该怎么做?
提前谢谢
最佳答案
使用send
item = Item.find(id)
if item.send(:"is_#{type}") == true
item.send(:"is_#{type}=", false)
end
关于ruby-on-rails - 将字符串插入 ruby 方法的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12683173/