我目前正在尝试重构一些重复的代码。有一个说法给我带来了最大的麻烦:
some_class = double("some_class", some_method?: true)
我需要用变量名替换 some_method 。但是,以下内容将不起作用。以下只是我正在尝试做的事情的一个示例。
some_method = unique_method
some_class = double("some_class", some_method?: true)
有谁知道替代的书写方式:
some_method? # where some_method is a var.
我需要能够打电话
some_class.some_method?
对于我的测试用例。
非常感谢任何帮助。预先感谢您。
最佳答案
用字符串包围它,应该没问题,或者你可以使用 :some_method? => value
语法。
def some_method?
puts "I am some_method? True"
end
some_hash_key = {:some_method? => true}
send(:some_method?)
my_variable = :some_method?
send(my_variable)
p some_hash_key
关于ruby - 在方法上指示 bool 值的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31496197/