Naming style guide in Ruby programming and ruby programming style guide
Name the identifier in English.
# bad - identifier using non-ascii characters заплата = 1_000 # bad - identifier is a Bulgarian word, written with Latin letters (instead of Cyrillic) zaplata = 1_000 # good salary = 1_000
Name the variables and methods in the form of "Snail ke_case.
# bad :'some symbol' :SomeSymbol :someSymbol someVar = 5 def someMethod ... end def SomeMethod ... end # good :some_symbol def some_method ... end
Snake case: punctuation is removed and spaces are replaced by single underscores. Normally the letters share the same case (either UPPER_CASE_EMBEDDED_UNDERSCORE or lower_case_embedded_underscore) but case can be mixed
Use CamelCase to name classes and modules. (Retain the method of using the thumbnail in upper case, such as HTTP,
RFC, XML)
# bad class Someclass ... end class Some_Class ... end class SomeXml ... end # good class SomeClass ... end class SomeXML ... end
Use the name of the file, such as hello_world.rb.
The purpose is to have only one class/module in each source file. The file name is named according to class/module, but the CamelCase is replaced by "Snail ke_case.
Use screaming_snke_case to name constants.
# bad SomeConst = 5 # good SOME_CONST = 5
Add a question mark (such as Array # empty?) at the end of the method name indicating the judgment (whether the method returns true or false ?).
The method does not return a Boolean value and should not end with a question mark.
This may cause potential "dangerous" method names (such as modifying self or parameter methods, exit! (Not like exit execution completion items) add an exclamation point at the end if there is a security version of this dangerous method.
# bad - there is not matching 'safe' method class Person def update! end end # good class Person def update end end # good class Person def update! end def update end end
If possible, define the corresponding security method (non-bang) based on the Dangerous Method (bang ).
class Array def flatten_once! res = [] each do |e| [*e].each { |f| res << f } end replace(res) end def flatten_once dup.flatten_once! end end
When reduce is used in a short block, name the parameter | a, e | (accumulator, element ).
# Combines all elements of enum enumeration by applying a binary operation, specified by a block or a symbol that names a method or operator. # Sum some numbers (5 .. 10 ). reduce (: +) #=> 45 # reduce # Same using a block and inject (5 .. 10 ). inject {| sum, n | sum + n }#=> 45 # inject injection # Multiply some numbers (5 .. 10 ). reduce (1,: *) # => 151200 # Same using a block (5 .. 10 ). inject (1) {| product, n | product * n} # => 151200
When defining binary operators, name the parameters as other (<and [] are exceptions to this rule because they have different semantics ).
def +(other) # body omitted end
Map takes precedence over collect, find takes precedence over detect, select takes precedence over find_all, reduce takes precedence over inject, and size takes precedence over length. The above rules are not absolute. If the latter can improve the readability of the Code, use them. Method names with rhymes (such as collect, detect, and inject) inherit from the SmallTalk language, which are not very common in other languages. We recommend that you use select instead of find_all because the select statement works well with reject and its name is self explanatory.
Do not use count as an alternative to size. For objects other than the Enumerable Array, the entire set is iterated.
Determines its size.
# bad some_hash.count # good some_hash.size
Tends to use flat_map instead of the combination of map + flatten.
This does not apply to arrays with a depth greater than 2. For example, if users. first. songs = ['A', ['B', 'C'], map + flatten is used instead of flat_map.
Flat_map flates the array to a level, while flatten flates the entire array.
# bad all_songs = users.map(&:songs).flatten.uniq # good all_songs = users.flat_map(&:songs).uniq
Use reverse_each instead of reverse. each. Reverse_each does not allocate a new array and this is a good thing.
# bad array.reverse.each { ... } # good array.reverse_each { ... }