2006/2/12, jarnaud / prosodiemail.com <jarnaud / prosodiemail.com>:
> Hi all, let's say that I have a very nice:
>
> class Text
>  def initialize(text, bold="no", italic="no", color="black")
>    @text=text
>    @bold=bold
>    @italic=italic
>    @color=color
>  end
> end

I'd represent false by nil or false and not "no". That way you can
directly use the boolean value.

Text = Struct.new( :text, :bold, :italic, :color )

def Text(t,a)
  x = {:text=>t,:color=>:black}.update(a)
  Text.new(*(Text.members.map{|f| x[f.to_sym]}))
end

def Text2(t,a)
  x = a.inject(:text=>t,:color=>:black){|h,(k,v)| h[k]=v unless
k==:text || "no" == v;h }
  Text.new(*Text.members.map{|f| x[f.to_sym]})
end

irb(main):021:0> Text("foo", :bold=>true)
=> #<struct Text text="foo", bold=true, italic=nil, color=:black>
irb(main):022:0> Text2("foo", :bold=>true, :italic=>"no")
=> #<struct Text text="foo", bold=true, italic=nil, color=:black>

see also
http://www.rubygarden.org/ruby?KeywordArguments

HTH

robert

--
Have a look: http://www.flickr.com/photos/fussel-foto/