[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

OptionParser questions

Rich Morin

10/5/2006 7:04:00 PM

I'm a bit confused by the way that OptionParser works. In
the "Complete example" on


http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionP...

I see

opts = OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
...

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?

-r
--
http://www.cf... Rich Morin
http://www.cf.../resume rdm@cfcl.com
http://www.cf.../weblog +1 650-873-7841

Technical editing and writing, programming, and web development

2 Answers

F. Senault

10/5/2006 7:31:00 PM

0

Le 5 octobre 2006 à 21:03, Rich Morin a écrit :

> opts = OptionParser.new do |opts|
> opts.banner = "Usage: example.rb [options]"
> ...
>
> 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?

Not really. I'd say that example is badly written. The
OptionParser.new call can take a block, with one parameter. The name
between || is the name of that parameter, and is a variable local to the
block. I'd write something like this myself :

opts = OptionParser.new do |o|
o.banner = ...
...
end

(If the whole code is inside the block, you can even drop the first
assignment, too.)

> Is this a common way of doing things? Are
> there any caveats that must be observed in this situation?

I'm not good enough with the Ruby usages to reply here...

Fred
--
Sometimes everything is wrong. Now it's time to sing along. When your
day is night alone, (hold on, hold on) If you feel like letting go,
(hold on) When you think you've had too much of this life, well hang
on. (R.E.M., Everybody Hurts)

Patriot Games

10/20/2008 10:13:00 PM

0

On Mon, 20 Oct 2008 10:54:49 -0700 (PDT), Hugh Wood
<BurtonUrny@gmail.com> wrote:
>Slo' Joe: (FFLLLUUUSSSHHHH) Alright dammit just pick up the phone and
>tell that idiot the President will be right with him (slo' joe
>mumbles) (I knew that big eared dufus could'nt pour piss out of a boot
>if the directions were written on the heel).

BWAHAHAHAHAHAHAHAHAHAHA!!!!