text stringlengths 0 444 |
|---|
=== Ordered Hashes [[ordered-hashes]] |
Rely on the fact that as of Ruby 1.9 hashes are ordered. |
=== No Modifying Collections [[no-modifying-collections]] |
Do not modify a collection while traversing it. |
=== Accessing Elements Directly [[accessing-elements-directly]] |
When accessing elements of a collection, avoid direct access via `[n]` by using an alternate form of the reader method if it is supplied. |
This guards you from calling `[]` on `nil`. |
[source,ruby] |
---- |
# bad |
Regexp.last_match[1] |
# good |
Regexp.last_match(1) |
---- |
=== Provide Alternate Accessor to Collections [[provide-alternate-accessor-to-collections]] |
When providing an accessor for a collection, provide an alternate form to save users from checking for `nil` before accessing an element in the collection. |
[source,ruby] |
---- |
# bad |
def awesome_things |
@awesome_things |
end |
# good |
def awesome_things(index = nil) |
if index && @awesome_things |
@awesome_things[index] |
else |
@awesome_things |
end |
end |
---- |
=== `map`/`find`/`select`/`reduce`/`include?`/`size` [[map-find-select-reduce-include-size]] |
Prefer `map` over `collect`, `find` over `detect`, `select` over `find_all`, `reduce` over `inject`, `include?` over `member?` and `size` over `length`. |
This is not a hard requirement; if the use of the alias enhances readability, it's ok to use it. |
The rhyming methods are inherited from Smalltalk and are not common in other programming languages. |
The reason the use of `select` is encouraged over `find_all` is that it goes together nicely with `reject` and its name is pretty self-explanatory. |
=== `count` vs `size` [[count-vs-size]] |
Don't use `count` as a substitute for `size`. |
For `Enumerable` objects other than `Array` it will iterate the entire collection in order to determine its size. |
[source,ruby] |
---- |
# bad |
some_hash.count |
# good |
some_hash.size |
---- |
=== `flat_map` [[flat-map]] |
Use `flat_map` instead of `map` + `flatten`. |
This does not apply for arrays with a depth greater than 2, i.e. if `users.first.songs == ['a', ['b','c']]`, then use `map + flatten` rather than `flat_map`. |
`flat_map` flattens the array by 1, whereas `flatten` flattens it all the way. |
[source,ruby] |
---- |
# bad |
all_songs = users.map(&:songs).flatten.uniq |
# good |
all_songs = users.flat_map(&:songs).uniq |
---- |
=== `reverse_each` [[reverse-each]] |
Prefer `reverse_each` to `reverse.each` because some classes that `include Enumerable` will provide an efficient implementation. |
Even in the worst case where a class does not provide a specialized implementation, the general implementation inherited from `Enumerable` will be at least as efficient as using `reverse.each`. |
[source,ruby] |
---- |
# bad |
array.reverse.each { ... } |
# good |
array.reverse_each { ... } |
---- |
=== `Object#yield_self` vs `Object#then` [[object-yield-self-vs-object-then]] |
The method `Object#then` is preferred over `Object#yield_self`, since the name `then` states the intention, not the behavior. This makes the resulting code easier to read. |
[source,ruby] |
---- |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.