[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: OptionParser questions

Gavin Kistner

10/5/2006 7:15:00 PM

From: Rich Morin [mailto:rdm@cfcl.com]
> It looks like the "opts" object is being handed to the block,
> as well as being returned as the result of the "new" method.
> Is this the case? Is this a common way of doing things? Are
> there any caveats that must be observed in this situation?

It's a little goofy. What's happening here is that when the block is
called, the local 'opts' variable is getting a particular value. When
the block is done, however, the new method returns an instance of the
OptionParser object. In this case, they (probably) happen to be the same
instance, but it's not necessarily so.

I don't think there are any caveats, other than to realize that it
doesn't matter what you do to your block parameter inside the block,
because the resulting assignment is going to determine what the value of
that outer variable is.

def foo
1.upto( 3 ){ |i|
yield i
}
return "whee"
end

x = foo{ |x|
x = x*x
p x
}
p x
#=> 1
#=> 4
#=> 9
#=> "whee"