From: "Ara.T.Howard" Date: 2005-10-25T06:03:39+09:00 Subject: Re: A comparison by example of keyword argument styles On Tue, 25 Oct 2005, Louis J Scoras wrote: > Right, so this is probably a really dumb idea, but I was just having a > little bit of fun. It would be cool if you could define methods like this: > > define :foo do > required :bar, :baz > optional :request => 'useless' > named :side, > :meat => 'fish' > body do > puts "Good, I got to say #{foo}#{bar}." > puts "You thought of my third request was #{request}" > puts "For dinner we have #{meat}" > if side > puts "With a side of #{side}" > end > end > end > > foo 'hello ', 'world', :meat => 'steak', :side => 'potatoes' > > That is the first few times you wrote a definition like that. Then it would > get very old very quickly I'm assuming ;) harp:~ > cat a.rb require 'parseargs' include ParseArgs def meth *argv pa = parseargs(argv) do req_arg 'foo', 'bar' opt_arg 'request' => 'useless' opt_kw 'side' opt_kw 'meat' => 'fish' end puts <<-txt Good, I got to say #{ pa.foo } #{ pa.bar }. You thought of my third request was #{ pa.request } For dinner we have #{ pa.meat } txt if pa.side puts "With a side of #{ pa.side }" end end meth 'hello ', 'world', 'useless', :meat => 'steak', :side => 'potatoes' harp:~ > ruby a.rb Good, I got to say hello world. You thought of my third request was useless For dinner we have steak With a side of potatoes note. it's impossible to determine, if an argument is optional (your 'request' above) if the last hash passed __is__ that optional argument itself or if it's the set of keywords. therfore you must pass 'useless' for the request in this case - if you think about it the parsing is otheriwse impossible. cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | anything that contradicts experience and logic should be abandoned. | -- h.h. the 14th dalai lama ===============================================================================