Collezioni
# bad
arr = Array.new
hash = Hash.new
# good
arr = []
hash = {}# bad
STATES = %w(draft open closed)
# good
STATES = ["draft", "open", "closed"]# bad
{
foo: :bar,
baz: :toto
}
# good
{
foo: :bar,
baz: :toto,
}Last updated
# bad
arr = Array.new
hash = Hash.new
# good
arr = []
hash = {}# bad
STATES = %w(draft open closed)
# good
STATES = ["draft", "open", "closed"]# bad
{
foo: :bar,
baz: :toto
}
# good
{
foo: :bar,
baz: :toto,
}Last updated
# bad
{ :a => 1, :b => 2 }
# good
{ a: 1, b: 2 }# bad
{ a: 1, "b" => 2 }
# good
{ :a => 1, "b" => 2 }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)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# bad
[
1,
2]
{
a: 1,
b: 2}
# good
[
1,
2,
]
{
a: 1,
b: 2,
}