# Re: not sure what is wrong. strange error

**URL:** <https://www.ruby-forum.com/t/re-not-sure-what-is-wrong-strange-error/68840>\
**Category:** Ruby\
**Created:** [September 15, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/re-not-sure-what-is-wrong-strange-error/68840 "2006-09-15T20:45:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 15, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/re-not-sure-what-is-wrong-strange-error/68840/1 "2006-09-15T20:45:13Z")

</div>

From: Nathan S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> 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\>
