# Forum: Ruby a, b, c, d = .map{|x| x + o}

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2013-11-07 01:14
```I am doing some calculations, which often includes adding some offset to
a few values, i.e. moving a line segment (x1, y1, x2, y2).
Storing the values in an array makes not really sense, I don't want to

So I have a few statements like

a, b, c, d = a + o, b + o, c + o, d + o

I can replace that with

a, b, c, d = [a, b, c, d].map{|x| x + o}

which is slower, but "cleaner" in my opinion.
Is there another solution?```
on 2013-11-07 02:22
```Stefan Salewski wrote in post #1126646:
> I am doing some calculations, which often includes adding some offset to
> a few values, i.e. moving a line segment (x1, y1, x2, y2).
> Storing the values in an array makes not really sense, I don't want to
> write coord[0][1] instead of y1.
>
> So I have a few statements like
>
> a, b, c, d = a + o, b + o, c + o, d + o
>
> I can replace that with
>
> a, b, c, d = [a, b, c, d].map{|x| x + o}
>
> which is slower, but "cleaner" in my opinion.
> Is there another solution?

I think there's nothing wrong with writing

a += o
b += o
c += o
d += o

as an alternative to the first statement, it lines up vertically and
it's clear on a row-by-row basis what is happening to each variable;  it

Anything you do to write it more succinctly will almost certainly be
slower.

If ruby were more of a lisp, I could imagine that something like this
could be optimised in the compiler:

(a, b, c, d).map!{|x| x + o }

but it's not, and it isn't.

Edit:  here's a hacky hack, playing with this idea:
https://gist.github.com/phluid61/7347383```
on 2013-11-07 02:57
```Considering the slower approachs they could lead you to a code like this
...

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
end

p1 = Point[10,20]
p2 = Point[30,40]

[p1, p2].each { |p| p += 1}

Or you can go futher writing a class called PointCollection ...

And have something like ...

pc = PointCollection.new(p1, p2)

pc.increment_all_by(1)

And you can go on... to infinite and beyond...

But, I think your first example is just right (since you use it with
only 4 variables (not 100)).

Abinoam Jr.```
on 2013-11-07 13:11
```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.

>      @x += offset
>      @y += offset
>   end

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
http://blog.rubybestpractices.com/posts/rklemme/01...

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```
on 2013-11-07 15:52
```> def move(delta)
>   @x += delta
>   @y += delta
>   self
> end

+1 to Robert Klemme (and to the blog post)```
This topic is locked and can not be replied to.