Collections writing guide in Ruby and Ruby collections writing guide
Source: Internet
Author: User
Collection Writing Guide in Ruby, Ruby Collection Writing Guide
Tends to the literal representation of arrays and hashes (unless you need to pass parameters to their constructors).
# bad
arr = Array.new
hash = Hash.new
# good
arr = []
hash = ()
When you need an array of words (without spaces and special symbols), always use% w to define an array of strings. Apply this rule only to two or more arrays.
# bad
STATES = ['draft', 'open', 'closed']
# good
STATES =% w (draft open closed)
Use% i when you need an array of symbols (and do not need to maintain Ruby 1.9 compatibility). This rule applies only if the array has only two or more elements.
# bad
STATES = [: draft,: open,: closed]
# good
STATES =% i (draft open closed)
Avoid commas after the last item of an Array or Hash, especially if the entries are not on a single line.
# bad-easier to move / add / remove items, but still not preferred
VALUES = [
1001,
2020,
3333,
]
# bad
VALUES = [1001, 2020, 3333,]
# good
VALUES = [1001, 2020, 3333]
Avoid creating huge gaps in the array.
arr = []
arr [100] = 1 # now you have an array with lots of nils
When accessing the first or last element of an array, prefer to use first or last instead of [0] or [-1].
If you want to ensure that the elements are unique, use Set instead of Array. Set is more suitable for unordered and unique collection of elements. Collections have similar operations to array consistency and fast lookup of hashing.
Wherever possible, use symbols instead of strings as hash keys.
The new hash syntax of 1.9 is preferred when your hash key is a symbol.
# bad
hash = {: one => 1,: two => 2,: three => 3}
# good
hash = {one: 1, two: 2, three: 3}
Don't mix Ruby 1.9 hash syntax and hash in the form of arrows in the same hash literal. When you
The resulting keys are converted to arrow syntax when they are not symbols.
# bad
{a: 1, 'b' => 2}
# good
{: a => 1, 'b' => 2}
Use Hash # key? Without Hash # has_key? And Hash # value? Without Hash # has_value? Matz mentioned that the long form is considered deprecated.
# bad
hash.has_key? (: test)
hash.has_value? (value)
# good
hash.key? (: test)
hash.value? (value)
Use fetch when processing hash keys that should exist.
heroes = {batman: 'Bruce Wayne', superman: 'Clark Kent'}
# bad-if we make a mistake we might not spot it right away
heroes [: batman] # => "Bruce Wayne"
heroes [: supermann] # => nil
# good-fetch raises a KeyError making the problem obvious
heroes.fetch (: supermann)
When using fetch, use the second parameter to set the default value instead of using custom logic.
batman = {name: 'Bruce Wayne', is_evil: false}
# bad-if we just use || operator with falsy value we won't get the expected result
batman [: is_evil] || true # => true
# good-fetch work correctly with falsy values
batman.fetch (: is_evil, true) # => false
Try to use fetch to add blocks instead of setting defaults directly.
batman = {name: 'Bruce Wayne'}
# bad-if we use the default value, we eager evaluate it
# so it can slow the program down if done multiple times
batman.fetch (: powers, get_batman_powers) # get_batman_powers is an expensive call
# good-blocks are lazy evaluated, so only triggered in case of KeyError exception
batman.fetch (: powers) {get_batman_powers}
Use Hash # values_at when you need to retrieve a series of values from a hash continuously.
# bad
email = data ['email']
nickname = data ['nickname']
# good
email, username = data.values_at ('email', 'nickname')
Remember, in Ruby 1.9, the performance of hashing is no longer out of order. (Translator's Note: Ruby 1.9 will remember the sequence of element insertion)
While traversing a collection, do not modify the collection.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.