A, b, c, d = .map{|x| x + o}

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?

Stefan S. 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
depends entirely on your tastes.

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

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.
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 Thu, Nov 7, 2013 at 2:56 AM, Abinoam Jr. [email protected] 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

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

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

+1 to Robert K. (and to the blog post)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs