# Re: OptionParser questions

**URL:** <https://www.ruby-forum.com/t/re-optionparser-questions/71173>\
**Category:** Ruby\
**Created:** [October 5, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/re-optionparser-questions/71173 "2006-10-05T21:15:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 5, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/re-optionparser-questions/71173/1 "2006-10-05T21:15:33Z")

</div>

From: Rich M. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> 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”
