S
Stephan Mueller
Hello,
i am kind of a newbi, please forgive me if i am asking stale questions.
I want to adjust the initialization of an object according to the
parameters given to new.
My first idea was polymorphism I know from languages like java:
def initialize
puts("no param")
end
def initialize(p1) # p1 is an int
puts("do it like Beckham")
end
def initialize(p2) # p2 is a string
puts("do it like Ronaldo")
end
I understand of course that this can't work in ruby without strong
typing.
My next idea was to use named parameters:
def initialize(p1=nil, p2=nil)
unless p1.nil?
..
end
unless p2.nil?
..
end
end
# okay, let's make a baby!
MyClass.new(p1=500)
But as far as I could see in the pickaxe, there are no named parameters
in ruby (1.8 at least), right?
So please tell me, what is the ruby way to handle this kind of problem?
Use a single parameter and reflection?
Cheers,
- Thorsten
i am kind of a newbi, please forgive me if i am asking stale questions.
I want to adjust the initialization of an object according to the
parameters given to new.
My first idea was polymorphism I know from languages like java:
def initialize
puts("no param")
end
def initialize(p1) # p1 is an int
puts("do it like Beckham")
end
def initialize(p2) # p2 is a string
puts("do it like Ronaldo")
end
I understand of course that this can't work in ruby without strong
typing.
My next idea was to use named parameters:
def initialize(p1=nil, p2=nil)
unless p1.nil?
..
end
unless p2.nil?
..
end
end
# okay, let's make a baby!
MyClass.new(p1=500)
But as far as I could see in the pickaxe, there are no named parameters
in ruby (1.8 at least), right?
So please tell me, what is the ruby way to handle this kind of problem?
Use a single parameter and reflection?
Cheers,
- Thorsten