# Different methods for initializing one object?

**URL:** <https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905>\
**Category:** Ruby\
**Created:** [December 9, 2005, 10:01pm UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905 "2005-12-09T22:01:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kibleur.christophe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eada6e/32.png) [@kibleur.christophe](https://www.ruby-forum.com/u/kibleur.christophe)\
**Post date:** [December 9, 2005, 10:01pm UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905/1 "2005-12-09T22:01:25Z")

</div>

Hi,  
let’s say I’ve got a class Point2D to write, it must have 2 methods to  
initialize :

1. one with cartesian coordinates i.e : p = Point2D.new(x,y) given the  
numbers x and y;
2. a second with polar coordinates i.e : p = Point2D.new(R,theta) given  
the radius R\>0 and an angle theta;  
How can I manage this ?

Thanks.

---

<div class="post-metadata">

**Author:** ![kibleur.christophe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eada6e/32.png) [@kibleur.christophe](https://www.ruby-forum.com/u/kibleur.christophe)\
**Post date:** [December 9, 2005, 10:15pm UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905/2 "2005-12-09T22:15:36Z")

</div>

kibleur.christophe wrote:

> Hi,  
> let’s say I’ve got a class Point2D to write, it must have 2 methods to  
> initialize :
> 
> 1. one with cartesian coordinates i.e : p = Point2D.new(x,y) given the  
> numbers x and y;
> 2. a second with polar coordinates i.e : p = Point2D.new(R,theta) given  
> the radius R\>0 and an angle theta;  
> How can I manage this ?

One way would be to take keyword arguments and figure out which you were  
given.

class Point2D  
def initialize( params )  
if params.has\_key? :r and params.has\_key? :theta  
## initialize from polar  
elsif params.has\_key? :x and params.has\_key? :y  
## initialize from cartesian  
else  
raise ArgumentError.new( “You must provide either :r and :theta OR  
:x and :y” )  
end  
end  
end

cart = Point2D.new( :x =\> 42, :y =\> -4 )  
polar = Point2D.new( :r =\> 3.14159, :theta =\> (Math::PI/4) )

---

<div class="post-metadata">

**Author:** ![kibleur.christophe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eada6e/32.png) [@kibleur.christophe](https://www.ruby-forum.com/u/kibleur.christophe)\
**Post date:** [December 9, 2005, 10:29pm UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905/3 "2005-12-09T22:29:58Z")

</div>

> ```
> raise ArgumentError.new( "You must provide either :r and :theta OR 
> 
> ```
> 
> :x and :y" )  
> end  
> end  
> end
> 
> cart = Point2D.new( :x =\> 42, :y =\> -4 )  
> polar = Point2D.new( :r =\> 3.14159, :theta =\> (Math::PI/4) )

Hi Mike,

What you’ve proposed is a fine idea, but I think that it’s important to  
be clear about your terminology. Ruby doesn’t currently have keyword  
arguments. In the example that you’ve provided you are passing a hash  
with symbols for keys into the initialize method and _not_ using  
keywords.

Regards,  
Matthew J Desmarais

---

<div class="post-metadata">

**Author:** ![kibleur.christophe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eada6e/32.png) [@kibleur.christophe](https://www.ruby-forum.com/u/kibleur.christophe)\
**Post date:** [December 9, 2005, 10:34pm UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905/4 "2005-12-09T22:34:34Z")

</div>

desmarm wrote:

> What you’ve proposed is a fine idea, but I think that it’s important to  
> be clear about your terminology. Ruby doesn’t currently have keyword  
> arguments. In the example that you’ve provided you are passing a hash  
> with symbols for keys into the initialize method and _not_ using  
> keywords.

Oop, you’re of course correct. It’s more “I can’t believe it’s not  
keyword arguments”, now with 30% less calories than regular argument  
passing (yum, syntactic sugar!).

---

<div class="post-metadata">

**Author:** ![kibleur.christophe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eada6e/32.png) [@kibleur.christophe](https://www.ruby-forum.com/u/kibleur.christophe)\
**Post date:** [December 9, 2005, 10:46pm UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905/5 "2005-12-09T22:46:16Z")

</div>

You can also define alternate constructors:

class Point2D

```
class << self

  def new_xy( x, y )
    new.set_xy( x, y )
  end

  def new_rt( r, t )
    new.set_rt( r, t )
  end

end

def set_xy( x, y )
  @x, @y = x, y
end

def set_rt( r, t )
  @r, @t = r, t
end

```

end

p = Point2D.new\_xy( x, y )  
p = Point2D.new\_rt( R, theta )

T.

---

<div class="post-metadata">

**Author:** ![kibleur.christophe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eada6e/32.png) [@kibleur.christophe](https://www.ruby-forum.com/u/kibleur.christophe)\
**Post date:** [December 9, 2005, 10:50pm UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905/6 "2005-12-09T22:50:20Z")

</div>

Quoting Mike F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > the radius R\>0 and an angle theta;  
> > How can I manage this ?

If you are always storing cartesian coordinates internally, I’d  
suggest introducing a different factory method for creating points  
from polar coordinates. For example:

class Point2D  
attr\_accessor :x, :y

def initialize( x, y )  
@x, @y = x, y  
end

def self.new\_polar( r, theta )  
new( r \* Math::cos( theta ), r \* Math::sin( theta ) )  
end  
end

examples:

cart = Point2D.new( 42, -4 )  
polar = Point2D.new\_polar( 3.14159, Math::PI / 4 )

-mental

---

<div class="post-metadata">

**Author:** ![kibleur.christophe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eada6e/32.png) [@kibleur.christophe](https://www.ruby-forum.com/u/kibleur.christophe)\
**Post date:** [December 10, 2005, 12:03am UTC](https://www.ruby-forum.com/t/different-methods-for-initializing-one-object/49905/7 "2005-12-10T00:03:53Z")

</div>

Thanks for all your answers, in fact, my class was already written with  
the last mentioned code method.

class Point

attr\_accessor :abs, :ord, :name, :cart

def initialize(abs,ord,name="")  
@abs = abs  
@ord = ord  
@name = name  
@cart = true  
end

def to\_s  
“#@name : (#{@abs}, #{@ord})”  
end

def ==(unPoint)  
unPoint.abs = abs and unPoint.ord = ord  
end

def Point.new\_polaire(rho, theta, name)  
@cart = false  
@name = name  
unPoint = new(rho_cos(theta),rho_sin(theta),@name)  
return unPoint  
end

end

…but I dislike the calling syntax. I thought it was possible to write  
several constructors calls like in Java, ie :

```
void Move(Vector2D vector) {
    center.x += vector.x;
    center.y += vector.y;
}

void Move(double x,double y) {
    center.x += x;
    center.y += y;
}

```

So, my idea was bad : Ruby is not Java !
