Forum: Ruby difference between {} and do/end

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeff Schwab (Guest)
on 2009-02-20 17:30
(Received via mailing list)
I'm coming back to Ruby after a couple years away, and finding I've
forgotten some of the basics.  What's the difference between { ... }
blocks and do ... end blocks?  In particular, why does one of the
following work, while the other fails?

'%61lice'.gsub! /%(\d+)/ do $1.to_i(16).chr end # => "alice"
'%61lice'.gsub! /%(\d+)/ {  $1.to_i(16).chr   } # => SyntaxError
9377764fa3b08a1c7bf702d373ee653a?d=identicon&s=25 karlvonl (Guest)
on 2009-02-20 17:41
(Received via mailing list)
On Feb 20, 11:25 am, Jeff Schwab <j...@schwabcenter.com> wrote:
> I'm coming back to Ruby after a couple years away, and finding I've
> forgotten some of the basics.  What's the difference between { ... }
> blocks and do ... end blocks?  In particular, why does one of the
> following work, while the other fails?
>
> '%61lice'.gsub! /%(\d+)/ do $1.to_i(16).chr end # => "alice"
> '%61lice'.gsub! /%(\d+)/ {  $1.to_i(16).chr   } # => SyntaxError

It has to do with operator precedence. {...} has high precedence,
while do ... end has low precedence. So in your examples above, the
do...end block is pased to the gsub! method, while the {...} block is
passed to the regex. You can fix the second example using parens:

'%61lice'.gsub!(/%(\d+)/) {  $1.to_i(16).chr   } => "alice"
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeff Schwab (Guest)
on 2009-02-20 18:29
(Received via mailing list)
karlvonl wrote:
> while do ... end has low precedence. So in your examples above, the
> do...end block is pased to the gsub! method, while the {...} block is
> passed to the regex. You can fix the second example using parens:
>
> '%61lice'.gsub!(/%(\d+)/) {  $1.to_i(16).chr   } => "alice"

Thank you!
1bc63d01bd3fcccc36fb030a62039352?d=identicon&s=25 David Masover (Guest)
on 2009-02-20 23:31
(Received via mailing list)
Jeff Schwab wrote:
> I'm coming back to Ruby after a couple years away, and finding I've
> forgotten some of the basics.  What's the difference between { ... }
> blocks and do ... end blocks?

Another difference is that {} can be confused with a hash literal. I
can't think of a good example where it's actually ambiguous, though.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-02-20 23:47
(Received via mailing list)
David Masover wrote:
> Jeff Schwab wrote:
>> I'm coming back to Ruby after a couple years away, and finding I've
>> forgotten some of the basics.  What's the difference between { ... }
>> blocks and do ... end blocks?
>
> Another difference is that {} can be confused with a hash literal. I
> can't think of a good example where it's actually ambiguous, though.

Isn't

p {1=>2}

such an example? The parser decides that it's a block and barfs on the
=>.
This topic is locked and can not be replied to.