当您在Rails中看到这样的方法或变量之前带有下划线时,这在Ruby中是什么意思
submit_tag _('Enroll')
:notice => _('Update card.')
或者
_session_id
这些仅仅是大多数约定,还是暗示变量/方法的行为在功能上有所不同?
行为没有区别。这只是convert依。
但是,让我们仔细看看:
_('A string')
实际上是方法而不是变量。下划线方法由Gettext定义,并转换字符串。
@_foo
用于表示其他开发人员该变量有一些特殊之处,因此不应使用它。对于用于缓存值的变量,我看到了很多这种模式,例如:
def expensive_operation
@_expensive_operation ||= begin
# long running code...
end
end
下划线有时用于指示块中未使用变量。像这样:
a_hash.each do |_, value|
# do something with the value, not with the key
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句