Name an 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 variables and methods using the form snake_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 to single replaced. Normally the letters share the same case (either Upper_case_embedded_underscore or Lower_case_embedded_underscore) but the Case can be mixed
Names the classes and modules in the form of CamelCase (Camel-peak case). (Keep the initials capitalized in the same way as HTTP,
RFC, XML)
# bad
class SomeClass ...
End
class Some_class ...
End
class somexml ...
End
# good
class SomeClass ...
End
class somexml ...
End
Use Snake_case to name files, such as HELLO_WORLD.RB.
The purpose is to have only a single class/module in each source file. Follow Class/module to name the file name, but replace CamelCase with Snake_case.
Use Screaming_snake_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 that represents the judgment (method returns True or false).
method does not return a Boolean value and should not end with a question mark.
A method name that may cause potentially "dangerous" (such as modifying self or parameters, exit! (Not like exit execution completion) and so on) you should add an exclamation point at the end if there is a safe version of the Dangerous method.
# Bad-there is isn't matching ' safe ' method
class person
def update!
End
of
# Good
class person
def update
end
# good
class person
def update !
End
def update
end
If possible, define the corresponding security method (Non-bang) According to 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 you use reduce in a short block, the named parameter |a, e| (accumulator, Element).
#Combines all elements of enum enumeration by applying a binary operation, specified by a blocks or a symbol that names A or O Perator.
# Sum Some numbers
(5..10). Reduce (: +) #=> 45#reduce
# Same using a block and inject
(5..10). Inject {|s Um, n| Sum + n} #=> #inject注入
# Multiply some numbers
(5..10). Reduce (1,:*) #=> 151200
# Same Usin G A block
(5..10). Inject (1) {|product, n| product * n} #=> 151200
When defining the two-dollar operator, the parameter is named "Other" (<< and [] is an exception to this rule because they have different semantics).
def + (Other)
# body omitted
end
Map takes precedence over collect,find precedence over detect,select precedence over find_all,reduce precedence over length. The rules above are not absolute, and if you use the latter to improve the readability of your code, use them. The rhyming method names (such as Collect,detect,inject) are inherited from the SmallTalk language, and they are not very common in other languages. The use of select instead of Find_all is encouraged because the select is very good to use with reject, and its name is well self explanatory.
Do not use count as an alternative to size. Objects other than the enumerable Array will iterate over the entire collection
Determine its size.
# bad
some_hash.count
# good
some_hash.size
tend to use flat_map rather than map + flatten combinations.
This does not apply to arrays with depths greater than 2, for example, if users.first.songs = = [' A ', [' B ', ' C ']], use the combination of map + flatten instead of using Flat_map.
Flat_map the array flat one level, and flatten flattens 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 will not allocate a new array and this is a good thing.
# bad
array.reverse.each {...}
# good
array.reverse_each {...}