Michael Fellinger
2/12/2006 5:23:00 AM
Hey jarnaud,
Using an hash in that case looks like the most obvious option.
class Text
def initialize(text, options)
@text = text
@bold = options[:bold] || "no"
@italic = options[:italic] || "no"
@color = options[:color] || "black"
end
end
you use it like that:
foo.add text, {:color => "green"}
maybe somebody has some more fancy solution - just wait a bit :)
~~~~manveru
On Sunday 12 February 2006 14:12, jarnaud@prosodiemail.com wrote:
> 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
>
> class TextSet
> def initialize
> @texts=Array.new
> end
> def add(text)
> @texts.push(text)
> end
> end
>
> Let's say I dont want to have a attr_writer :italic, :bold, :color
> for TextSet because I want my code to look like
>
> foo = TextSet.new
> foo.add(Text.new("Hello, my name is"))
> foo.add(Text.new("Jerome"))
>
> as opposed to
>
> foo = TextSet.new
> text1=Text.new(Text.new("Hello, my name is"))
> text2=Text.new(Text.new("Jerome"))
> foo.add(text1)
> foo.add(text2)
>
> Ok, you follow me? Now let's talk about default value for the Text
> constructor.
>
> Let's say I want my name (Jerome) in green.
>
> I have to do foo.add(Text.new("Hello, my name is", "no, "no, "green")),
> right?
>
> Question: I'd like to get rid of the useless "no", "no".
> Is there a way to specify, that I want an instance of the object with
> the default parameters except for one?
>
> Thanks for your ideas