[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Using a block to surround a string?

seebs

7/4/2007 12:30:00 AM

In message <c6c1a0550707031707h1acf4ad8m6758b9c400f128c3@mail.gmail.com>, "Matt Greer" writes:
>And so I take it with blocks, they are given a new reference to the objects
>in question and therefore assigning to the variables in the block has no
>effect on the method. ie

Right.

>def around_string(string, &block)
> b = nil
> a = nil
> yield(b,a)
> "#{b}#{string}#{a}"
>end
>
>around_string('center') { |b, a| b = 'left'; a = 'right' }
>
>is worthless because in the block I'm merely assigning objects to local
>references that go out of scope once the block exits?

Exactly. The block's return in this case is just the string 'right' (the last
expression in the block).

You could easily do

a, b = yield

and call with

{ 'left', 'right' }

-s

1 Answer

Ivan V.

7/4/2007 3:38:00 PM

0

Everyone wrote:
> Very useful replies!

I've learned plenty from all your responses, and based on your input I'm
changing the way my code works.

Thanks a lot!

- Ivan V.

--
Posted via http://www.ruby-....