From: Todd Benson Date: 2007-10-11T19:43:41+09:00 Subject: Re: Why does this use a block On 10/11/07, Anonymous wrote: > So I'm new to Ruby and I'm trying to use OptionParser to parse in some > command line arguments. I looked at some documentation on OptionParser and > there is something that's confusing me to no end. In all the examples a > block is being passed in to the constructor of the OptionParser object. The > constructor simply makes a yield call, passing self as an argument. > > What is the point of this? Can we not convert > > opts = OptionParser.new() {|opts| body} > > To > > opts = OptionParser.new() > body > > ? This seems more natural to me. But the former seems like a common Ruby > idiom so I'm wondering what I am missing. > > Thanks, > > Jaimrk You are asking a bunch of questions here, but I'll try my hand at one of them. yield self It pretty much does exactly what the statement says it does. Why would you do this upon initializing an object? I suppose, because you might want something else to accomplish something during initialization. Blocks also give you control of scope and give the programmer a sense of finalization. File#open for example. Todd