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?
Variable names should not start with underscore ( _ ) or dollar sign ( $ ) characters, even though both are allowed. This is in contrast to other coding conventions that state that underscores should be used to prefix all instance variables. Variable names should be short yet meaningful.
The first answer is on the level of the Ruby language: there is no use of the underscore in Ruby. It has no meaning. It is just a legal identifier like any other legal identifier. It could have been named foo or bar instead, and it would not in any way change the meaning of this code.
General naming conventions in RubyClass names and module names use PascalCase. For example, ApplicationController. Method and variable names use snake_case. For example, attr_accessor.
The underscore before a variable name _val is nothing more than a convention. In C#, it is used when defining the private member variable for a public property.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With