轨(或Ruby):是/否,而不是真/假 [英] Rails (or Ruby): Yes/No instead of True/False
本文介绍了轨(或Ruby):是/否,而不是真/假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以很容易地编写一个函数,并把它的应用控制器,但我宁愿没有,如果有别的东西已经这样做了。基本上我想有这样的:
I know I could easily write a function and put it in the application controller, but I'd rather not if there is something else that does this already. Basically I want to have something like:
>> boolean_variable?
=> true
>> boolean_variable?.yesno
=> yes
>> boolean_variable?.yesno.capitalize
=> Yes
有没有这样的事情已经在Rails框架?
is there something like this already in the Rails framework?
推荐答案
没有这种内置的辅助存在,但它十分容易实现:
No such built-in helper exists, but it's trivially easy to implement:
class TrueClass
def yesno
"Yes"
end
end
class FalseClass
def yesno
"No"
end
end
这篇关于轨(或Ruby):是/否,而不是真/假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文