[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

difference between {} and do/end

Jeff Schwab

2/20/2009 4:26:00 PM

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
4 Answers

Karl von Laudermann

2/20/2009 4:37:00 PM

0

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"

Jeff Schwab

2/20/2009 5:21:00 PM

0

karlvonl wrote:
> 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"

Thank you!

David Masover

2/20/2009 10:30:00 PM

0

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.

Joel VanderWerf

2/20/2009 10:46:00 PM

0

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 =>.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407