Re: OptionParser questions

From: Rich M. [mailto:[email protected]]

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”

x = foo{ |x|
x = x*x
p x
p x
#=> 1
#=> 4
#=> 9
#=> “whee”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs