From: Lars Christensen Date: 2001-08-17T01:31:07+09:00 Subject: [ruby-talk:19908] Re: The GUI poll is in, and the resultsaresurprising On Fri, 17 Aug 2001, Hal E. Fulton wrote: > > module Fox > > class FXMainWindow > > def FXMainWindow.identifier(*symbols) > > instance_eval "#{symbols.join ', '}, ID_LAST = > > enum(Fox::FXMainWindow::ID_LAST, #{symbols.length + 1})" > > end > > end > > end > > Hmm. Can you explain this? Basically, it is a wrapper around the original approach. It builds a string like you would otherwise have to do yourself and evaluates it. Here is an example of a custom instance variable declarator, equivalent to attr_reader: class Module def myattr_reader(*symbols) symbols.each { |sym| module_eval %Q{ def #{sym} @#{sym} end } } end end class Foo myattr_reader :bar, :baz def initialize @bar = 2 end end f = Foo.new p f.bar #=> 2 p f.baz #=> nil > > > If some future version of Ruby supports keyword arguments, the situation > > > will be even better. > > > > Great! By why keep the arguments if you can do without them. When you use > > them, the expression becomes unreadable. You still won't be able to > > remember with the 5th argument was about. The only exception is the use of > > named arguments (by symbol or string, which is possible with today's > > ruby!): > > When Lyle said "keyword arguments" he meant the same as you mean > by "named arguments." Not exactly, but its close. If I am not mistaken, keyword arguments will allow this code: def func(size = 10, weight = 20, color = 'red') ... end func(weight => 10, color => 'blue') While, with current ruby, you would have to do this: def func(hashargs) size = hashargs[:size] || 10 weight = hashargs[:weight] || 20 color = hashargs[:color] || 'red' ... end func(:weight => 10, :color => 'blue') Which is not as nice as keyword arguments :-) -- Lars Christensen, larsch@cs.auc.dk