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
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.
[snip]
So please tell me, what is the ruby way to handle this kind of problem?
Use a single parameter and reflection?
We can ask for a variable number of arguments, then check for the various options. Something like:
def initialize( *args )
if args.empty?
puts "no param"
elsif args.size == 1 and args.first.is_a? Integer
puts "do it like Beckham"
elsif args.size == 1 and args.first.is_a? String
puts "do it like Ronaldo"
else
raise ArgumentError, "Invalid initialization for #{self.class}."
end
end
Hope that helps.
James Edward Gray II
···
On Aug 6, 2005, at 1:01 PM, Stephan Mueller wrote:
What you are talking about is really method overloading not
polymorphism in my opinion.
My take on the "ruby way" is to not overload methods by type
like you can easily do in java. Instead you just make multiple
distinct methods with each method taking its own "type". This
give maximal flexibility with duck-typing.
There is no reason you can't make multiple new type class
methods that call the normal "new" method - you wouldn't need
an initialize in that case.
The closest you can come to named parameters is by using a hash
(right now, at least):
def test(hash)
if hash[:p1]
...
elsif hash[:p2]
...
end
end
test(:p1=>500)
In ruby 1.9 you can use the shortcut test(p1: 500) when you
want the key to be a symbol.
···
--- Stephan Mueller <d454d@web.de> wrote:
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?
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs
Hi Eric,
* Eric Mahurin <eric_mahurin@yahoo.com> [050806 20:25]:
What you are talking about is really method overloading not
polymorphism in my opinion.
Yes, I agree.
My take on the "ruby way" is to not overload methods by type
like you can easily do in java. Instead you just make multiple
distinct methods with each method taking its own "type". This
give maximal flexibility with duck-typing.
There is no reason you can't make multiple new type class
methods that call the normal "new" method - you wouldn't need
an initialize in that case.
Something like this?
class MyClass
def new_s(str)
me = super
# do sth with str
return me
end
def new_i(int)
me = super
# do sth with int
return me
end
...
The closest you can come to named parameters is by using a hash
(right now, at least):
def test(hash)
if hash[:p1]
...
elsif hash[:p2]
...
end
end
test(:p1=>500)
Okay, looks quite good for me. Not much more effort than real named
params.
Thanks!
Steph.
Hi James,
* James Edward Gray II <james@grayproductions.net> [050806 20:06]:
We can ask for a variable number of arguments, then check for the
various options. Something like:
def initialize( *args )
if args.empty?
puts "no param"
elsif args.size == 1 and args.first.is_a? Integer
puts "do it like Beckham"
elsif args.size == 1 and args.first.is_a? String
puts "do it like Ronaldo"
else
raise ArgumentError, "Invalid initialization for # {self.class}."
end
end
Hope that helps.
Yes, thanks a lot. Though i think i like the approach using a hash
mentioned in another post a little bit more because it can do the same as
this one plus some sort of named params. It depends on the precise
situation I guess.
Cheers,
Steph.
I was thinking something like this (I don't think the above
does what you want):
class MyClass
def self.new_s(s)
obj = new
# do sth with s and obj
obj
end
def self.new_i(i)
obj = new
# do sth with i and obj
obj
end
end
···
--- Stephan Mueller <d454d@web.de> wrote:
Hi Eric,
* Eric Mahurin <eric_mahurin@yahoo.com> [050806 20:25]:
> What you are talking about is really method overloading not
> polymorphism in my opinion.
Yes, I agree.
> My take on the "ruby way" is to not overload methods by
type
> like you can easily do in java. Instead you just make
multiple
> distinct methods with each method taking its own "type".
This
> give maximal flexibility with duck-typing.
>
> There is no reason you can't make multiple new type class
> methods that call the normal "new" method - you wouldn't
need
> an initialize in that case.
Something like this?
class MyClass
def new_s(str)
me = super
# do sth with str
return me
end
def new_i(int)
me = super
# do sth with int
return me
end
...
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs
Comming from a C++ background I was also facing the same issue with
methods, not initialize specifically.
I found myself creating methods like:
* delete_object_by_class(someclass)
* delete_object_by_class_name(classname)
* delete_object_by_name(name)
* etc.
The people on #ruby-lang helped me understand closures a bit better.
So now instead of doing: delete_object_by_class(someclass)
I do, delete_object{|o| o.class == someClass}
By passing the test condition to the method through a block I cut out
quite alot of bad looking code.
···
On 8/7/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
--- Stephan Mueller <d454d@web.de> wrote:
> Hi Eric,
>
> * Eric Mahurin <eric_mahurin@yahoo.com> [050806 20:25]:
>
> > What you are talking about is really method overloading not
> > polymorphism in my opinion.
>
> Yes, I agree.
>
> > My take on the "ruby way" is to not overload methods by
> type
> > like you can easily do in java. Instead you just make
> multiple
> > distinct methods with each method taking its own "type".
> This
> > give maximal flexibility with duck-typing.
> >
> > There is no reason you can't make multiple new type class
> > methods that call the normal "new" method - you wouldn't
> need
> > an initialize in that case.
>
> Something like this?
>
> class MyClass
>
> def new_s(str)
> me = super
> # do sth with str
> return me
> end
>
> def new_i(int)
> me = super
> # do sth with int
> return me
> end
>
> ...
I was thinking something like this (I don't think the above
does what you want):
class MyClass
def self.new_s(s)
obj = new
# do sth with s and obj
obj
end
def self.new_i(i)
obj = new
# do sth with i and obj
obj
end
end
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs
--
Reyn Vlietstra
Hi Eric,
* Eric Mahurin <eric_mahurin@yahoo.com> [050807 04:00]:
I was thinking something like this (I don't think the above
does what you want):
class MyClass
def self.new_s(s)
obj = new
# do sth with s and obj
obj
end
def self.new_i(i)
obj = new
# do sth with i and obj
obj
end
end
You are right. My code was plain wrong. Without using self.new... the
method is unknown to Class and super can't work because in Class there is
no new_i or new_s. Ouch!
Your code works as intended. Thanks!
Steph.