V.polar = rho, theta

Hi, I’m using the Vector class and needed a way to set the vector with
polar coordinates. My solution:

class Vector
def set_polar(rho,theta)
@elements[0] = rho * Math.cos(theta)
@elements[1] = rho * Math.sin(theta)
end
end

But it struck me that defining polar=(rho,theta) would be more
ruby-like, and I expected to call it like so: v.polar = 1, 3.14
That doesn’t work, though. This does, but it’s ugly:

irb(main):020:0> v = Vector[0,0]
Vector[0, 0]
irb(main):021:0> v.send(:polar=,1,3.14)
0.001592652916
irb(main):022:0> v.inspect
"Vector[-0.9999987317, 0.001592652916]"

Is there another way, or what should I do?

v.polar(1, 3.14)

or

v.polar= [1,3.14] # polar takes an array as argument

or name the method set_polar.

Regards,

Michael

···

On Thu, 2002-11-07 at 22:09, Hans Fugal wrote:

Hi, I’m using the Vector class and needed a way to set the vector with
polar coordinates. My solution:

class Vector
def set_polar(rho,theta)
@elements[0] = rho * Math.cos(theta)
@elements[1] = rho * Math.sin(theta)
end
end

But it struck me that defining polar=(rho,theta) would be more
ruby-like, and I expected to call it like so: v.polar = 1, 3.14
That doesn’t work, though. This does, but it’s ugly:

irb(main):020:0> v = Vector[0,0]
Vector[0, 0]
irb(main):021:0> v.send(:polar=,1,3.14)
0.001592652916
irb(main):022:0> v.inspect
“Vector[-0.9999987317, 0.001592652916]”

Is there another way, or what should I do?

Is there a reason why Ruby doesn’t treat this case like like it treats
multiple assignment to a variable?

Paul

···

On Fri, Nov 08, 2002 at 06:09:17AM +0900, Hans Fugal wrote:

But it struck me that defining polar=(rho,theta) would be more
ruby-like, and I expected to call it like so: v.polar = 1, 3.14
That doesn’t work, though.

Thanks for the suggestions. I actually went with a different idea,
inspired by Complex:

 def Vector.polar(rho, theta)
Vector[rho * Math.cos(theta), rho * Math.sin(theta)]
 end

v = Vector.polar(1,3.14)

But it struck me that defining polar=(rho,theta) would be more
ruby-like, and I expected to call it like so: v.polar = 1, 3.14
That doesn’t work, though.

Is there a reason why Ruby doesn’t treat this case like like it treats
multiple assignment to a variable?

It does treat it the same way as multiple assignment to a variable - it
passes a single array argument. However initial example defined polar= as
accepting 2 arguments. That’s why it did not work.

Gennady.

···

----- Original Message -----
From: “Paul Brannan” pbrannan@atdesk.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, November 07, 2002 1:27 PM
Subject: Re: v.polar = rho, theta

On Fri, Nov 08, 2002 at 06:09:17AM +0900, Hans Fugal wrote:

Paul

It is slightly odd – but if polar=(rho, theta) were changed to
polar=(*rhotheta), then it would work as expected.

class VPtest
def polar=(*a)
p a.flatten
end
def initialize; end
end

v = VPtest.new
v.polar = 5, 6

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.07 at 18.48.46

···

On Fri, 8 Nov 2002 06:27:07 +0900, Paul Brannan wrote:

On Fri, Nov 08, 2002 at 06:09:17AM +0900, Hans Fugal wrote:

But it struck me that defining polar=(rho,theta) would be more
ruby-like, and I expected to call it like so: v.polar = 1, 3.14
That doesn’t work, though.
Is there a reason why Ruby doesn’t treat this case like like it
treats multiple assignment to a variable?

Hmm, that makes sense. So:
def polar=(x)
raise ArgumentError if x.size != 2
@rho, @theta = *x
end

would work. The only thing I don’t like about this is that the user has
to explictly test the number of arguments that were passed in (something
that it seems the interpreter should do for you). So the following
would instead work:

def polar=(x)
set_polar(*x)
end

def set_polar(rho, theta)
@rho = rho
@theta = theta
end

but this requires a second method call.

Are there any other options?

Paul

···

On Fri, Nov 08, 2002 at 06:54:29AM +0900, Gennady F. Bystritsky wrote:

It does treat it the same way as multiple assignment to a variable - it
passes a single array argument. However initial example defined polar= as
accepting 2 arguments. That’s why it did not work.