Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

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' } # 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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #meaning #ruby
ADD COMMENT
Topic
Name
2+3 =