by jashkenas · 16y ago · 0 comments
Here are some of the variants:
method({ ... }) # hash
method { ... } # block, even though a hash would be possible.
method = { ... } # hash
method a, b, :c => d # implicit hash
method a, b { ... } # SyntaxError
method(a, b) { ... } # block
Only #2 in that list is really a problem, because there are situations where you'd legitimately like to use both.The other one to watch out for is precedence of {} vs do; end. Example:
> x = [1,2].map do |i| i*2; end
=> [2, 4]
> puts [1,2].map do |i| i*2; end
1
2
=> nil
> puts [1,2].map { |i| i*2}
2
4
=> nil x = [1, 2].map (i) -> i * 2
... is without optional parens. x = [1, 2].map((i) -> i * 2)
... is with them. configure start: 10, end: 25
... is the syntax for calling a method and passing an implicit hash -- and with the optional braces and parens added, it's closer to JavaScript... configure({start: 10, end: 25})