In Ruby, what's the difference between {}
and []
?
{}
seems to be used for both code blocks and hashes.
Are []
only for arrays?
The documention isn't very clear.
Square brackets indicate character classes in Ruby regular expressions.
From Programming Ruby: Braces have a high precedence; do has a low precedence. If the method invocation has parameters that are not enclosed in parentheses, the brace form of a block will bind to the last parameter, not to the overall invocation. The do form will bind to the invocation.
inject(:+) is not Symbol#to_proc, :+ has no special meaning in the ruby language - it's just a symbol.
Nope. You need to use end instead of a } .
It depends on the context:
When on their own, or assigning to a variable, []
creates arrays, and {}
creates hashes. e.g.
a = [1,2,3] # an array b = {1 => 2} # a hash
[]
can be overridden as a custom method, and is generally used to fetch things from hashes (the standard library sets up []
as a method on hashes which is the same as fetch
)
There is also a convention that it is used as a class method in the same way you might use a static Create
method in C# or Java. e.g.
a = {1 => 2} # create a hash for example puts a[1] # same as a.fetch(1), will print 2 Hash[1,2,3,4] # this is a custom class method which creates a new hash
See the Ruby Hash docs for that last example.
This is probably the most tricky one - {}
is also syntax for blocks, but only when passed to a method OUTSIDE the arguments parens.
When you invoke methods without parens, Ruby looks at where you put the commas to figure out where the arguments end (where the parens would have been, had you typed them)
1.upto(2) { puts 'hello' } # it's a block 1.upto 2 { puts 'hello' } # syntax error, ruby can't figure out where the function args end 1.upto 2, { puts 'hello' } # the comma means "argument", so ruby sees it as a hash - this won't work because puts 'hello' isn't a valid hash
Another, not so obvious, usage of []
is as a synonym for Proc#call and Method#call. This might be a little confusing the first time you encounter it. I guess the rational behind it is that it makes it look more like a normal function call.
E.g.
proc = Proc.new { |what| puts "Hello, #{what}!" } meth = method(:print) proc["World"] meth["Hello",","," ", "World!", "\n"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With