Not sure what is wrong. strange error

It's too bad Ruby does not allow overloaded methods. It makes
more sense,
IMHO, in this case to have two constructors, each of which perform two
different 'functions', rather than having if/else/case
statements inside
the constructor with default arguments.

'Constructors' in ruby are just initializers that get run after the
internal 'new' allocates the object. The only thing special about
#initialize is that it happens to get called after you create a new
instance.

You can create as many different 'constructor' methods of the class as
you like. It's not parameter overloading, but (in my opinion) that's a
good thing.

class Circle
  attr_accessor :radius

  def initialize( radius )
    @radius = radius
  end

  def self.withradius( r )
    self.new( r )
  end

  def self.witharea( area )
    self.new( Math.sqrt( area / Math::PI ) )
  end

  def self.withcircumference( circumference )
    self.new( circumference / 2 / Math::PI )
  end
  
  def self.clone( circle )
    self.new( circle.radius )
  end

  def area
    Math::PI * @radius ** 2
  end

  def circumference
    2 * Math::PI * @radius
  end
end
  
p c1=Circle.new( 10 ),
  c2=Circle.witharea( 100 ),
  c3=Circle.withcircumference( 50 ),
  c4=Circle.clone( c1 )

#=> #<Circle:0x2833504 @radius=10>
#=> #<Circle:0x2832e60 @radius=5.64189583547756>
#=> #<Circle:0x2832c94 @radius=7.95774715459477>
#=> #<Circle:0x2832c08 @radius=10>

ยทยทยท

From: Nathan Smith [mailto:nsmith5@umbc.edu]