On Thu, Nov 7, 2013 at 2:56 AM, Abinoam Jr. <abinoam / gmail.com> wrote:
> Considering the slower approachs they could lead you to a code like this ...

I agree to the approach to create a specific class for coordinates.

> class Point
>   def initialize(x,y)
>     @x, @y = x, y
>   end
>
>   def self.[](x,y)
>      new(x,y)
>   end
>
>   def +(offset)
>      @x += offset
>      @y += offset
>   end

That's a bad implementation.  To start with this method should return
a Point.  Then, modifying in place does not fit well with how Ruby
handles operators. And handling of argument types is also not in line
with what Ruby does. I blogged about this a while ago:
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

I'd rather have a method "move" which does the transformation:

def move(delta)
  @x += delta
  @y += delta
  self
end

> end

You can achieve the same by

Point = Struct.new :x, :y

(apart from operator + of course).

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/