From: Alexy Khrabrov Date: 2007-10-25T22:35:52+09:00 Subject: Re: overcoming dynamic block evaluation > On Oct 25, 2007, at 5:15 PM, Robert Klemme wrote: > Unfortunately I don't have time to go through all of this, but did you > consider to define a block with the default options like this? > > DEFAULT_OPTS = lambda do |opts| > opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| > options.verbose = v > end > ... > end > > ... > > opts = OptionParser.new do |opts| > DEFAULT_OPTS[opts] > > opts.on("--type [TYPE]", [:text, :binary, :auto], > "Select transfer type (text, binary, auto)") do |t| > options.transfer_type = t > end > > end Well, I need to add textual reporting and report on all options, both set and unset. By "default" options I mean not those common to all scripts, but those which are not mentioned explicitly on the command line, thus their blocks never get triggered. So we cannot add reporting lines there as puts. In my solution reporting lines are added to an enclosing unless statement, and they are added to a list which is walked in the reporting loop later. The question really is about overcoming the fact that the opt.on blocks are evaluated later and my initial trick of referring to options on a uniform way as o stumbles upon that deferred evaluation, when o was long redefined. Cheers, Alexy