Another important difference between { } and do end is that { } has a higher precedence than do end. Like the documentation says:
do end has lower precedence than { } so:
method_1method_2{# ...}
Sends the block to method_2 while:
method_1method_2do# ...end
Sends the block to method_1.
The above is only relevant when omitting parentheses, but is still useful to know.
Jumping ahead to the Proc behavior with arguments, another thing that might be useful to know is that objects that have a #to_ary method (only arrays in core) will automatically be spread (splat) across the argument list if it is the sole argument to the proc. (Similar to what blocks do when an array is passed as the sole argument.)
pair=[3,4]proc_add=proc{|a,b|a+b}proc_add.(pair)#=> 7 # auto spread/splat across proc argumentsproc_add.(*pair)#=> 7lambda_add=->(a,b){a+b}lambda_add.(pair)#=> ArgumentError (wrong number of arguments (given 1, expected 2))lambda_add.(*pair)#=> 7pair.then{|a,b|a+b}#=> 7 # auto spread/splat across block arguments
I seem to recall mentioning the first item somewhere, if not in this article. The second I always tend to forget, and there was one point that that behavior was broken for Lambdas and I had to use procs instead.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another important difference between
{ }
anddo end
is that{ }
has a higher precedence thando end
. Like the documentation says:The above is only relevant when omitting parentheses, but is still useful to know.
Jumping ahead to the Proc behavior with arguments, another thing that might be useful to know is that objects that have a
#to_ary
method (only arrays in core) will automatically be spread (splat) across the argument list if it is the sole argument to the proc. (Similar to what blocks do when an array is passed as the sole argument.)I seem to recall mentioning the first item somewhere, if not in this article. The second I always tend to forget, and there was one point that that behavior was broken for Lambdas and I had to use procs instead.