i'm learning ruby book based on ruby 1.9 i'm using 2.2
i have 2 classes (from book)
class point def initialize(x,y) @x,@y = x,y end end class point3d < point def initialize(x,y,z) super(x,y) @z = z end end
however when call point3d.new(0,1,2)
ruby interpreter raises argumenterror
says wrong number of arguments (3 2)
.
why happening?
edit
i've omitted point class in require
d file.
i've tried call
require "./point.rb" point = point.new(0,1) puts point
which work expected
then added point3d class described on , tried call
require "./point.rb" class point3d < point ... end point1 = point3d.new(0,1,2) puts point1
which raises argumenterror.
if put 2 classes in same file (point3d.rb) problem not happen, if put point3d class in point.rb error remains.
i understood problem was.
the book made example on how create initialize
overload
class point def initialize(x,y) @x,@y = x,y end def self.new(x,y) #other stuff super end end
and missing super
call point3d
i've commented self.new
, point
working expected.
it interesting note if add methods point
in file point.rb this
class << point def to_s "foo" end end
point.rb works correctly.
Comments
Post a Comment