Ruby variable name with double underscores

混江龙づ霸主 提交于 2020-01-10 19:13:25

问题


Sometimes I see variable names with double underscore in the beginning and the end. For example:

Article.__elasticsearch__

Is there some naming convention related to double underscores in Ruby variable names?


回答1:


An initial underscore or double underscore basically indicates "special/avoid overwrite" --meaning it's meant to reduce the likelihood that someone else might define a method/attribute of the same name. The most common occurrence is __send__.

From Ruby Forum




回答2:


The author of the ElasticSearch gem made the wrong call IMO. At the end of the thread, Avdi Grimm, who is well-known in the Ruby community, disagrees with the OP.

There's a reason you hadn't seen it yet and that it looks odd to you. It's because it's unidiomatic.



来源:https://stackoverflow.com/questions/23270937/ruby-variable-name-with-double-underscores

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!