Ruby on Rails:如何将变量设置为常量,常量名称的一部分可以更改? [英] Ruby on Rails: How do I set a variable to a constant, where part of the name of the constant can change?
本文介绍了Ruby on Rails:如何将变量设置为常量,常量名称的一部分可以更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做
current_user.field = User::??????????
?
??????????
是我想要的样子
这就是我想要做的
Given /^"([^\"]*)" is a(?:|n) "([^\"]*)"$/ do |arg1, arg2|
cur_user = User.find(:first, :conditions => ['name = ?', arg1])
cur_user.update_attributes(:role => User::arg2.constantize)
end
虽然 constantize 不适用于此用途,但我知道它可以在 Object.var.constantize 上下文中使用
While constantize does't work for this use, I do know that it would work In Object.var.constantize context
推荐答案
要么使用:
"User::#{arg2}".constantize
或
User.const_get(arg2)
这篇关于Ruby on Rails:如何将变量设置为常量,常量名称的一部分可以更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文