I have a class that I want to have a number of overloaded constructors
for.
I tried doing something like this:
class GravPoint
def initialize()
this(0, 0, 0, 0, 0)
end
def initialize(x, y, strength)
this(x, y, strength, 0, 0)
end
def initialize(x, y, strength, ctime, life)
@x, @y, @strength, @ctime, @life = x, y, strength, ctime, life
end
end
When I then run GravPoint.new() I get the following error:
GravPoint.rb:23:in `initialize’: wrong number of arguments (0 for 5)
(ArgumentError)
I think I understand the error, it’s expecting 5 arguments (based on the
last constructor) but I don’t the “Ruby” way to set this up. What am I
missing?
I was able to get this to work, but was hoping that I could do it in a
format closer to the above to make it a little more flexible to use.
class GravPoint
def initialize(x = 0, y = 0, strength = 0, ctime = 0, life = 0 )
@x, @y, @strength, @ctime, @life = x, y, strength, ctime, life
end
end
Kyle H.
[email protected]