meaning of ruby code example
Example 1: meaning of {} in ruby
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' }
1.upto 2 { puts 'hello' }
1.upto 2, { puts 'hello' }
Example 2: meaning of {} in ruby
When on their own, or assigning to a variable, [] creates arrays, and {} creates hashes. e.g.
a = [1,2,3]
b = {1 => 2}
[] 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)
a = {1 => 2}
puts a[1]