-2

Is it possible to duplicate a float number array as a variable to a new variable in ruby with sketchup? I have tried both .clone and .dup, but no luck (see below).

a = [1.1,2.2,3.3]
b = [a.dup,a.dup,a.dup] #Returns "TypeError: can't dup Float"
b = [a.clone,a.clone,a.clone] #Returns "TypeError: can't clone Float"

Any other ways of duplicating an arrayed variable containing floats in ruby with sketchup?

EDIT: This is what I am trying to do:

a = [1.1,2.2,3.3]
x = [4.4,5.5,6.6]
b = [a,x]
b[0][1] += 1.1
b[1][1] += 1.1

so that a == [1.1,2.2,3.3], x == [4.4,5.5,6.6] and b == [[1.1,3.3,3.3],[4.4,6.6,6.6]]

I now realise that both .clone and .dup work in Ruby itself (Thanks to Amadan and Sami Kuhmonen)

Poyda
  • 84
  • 11

1 Answers1

0

I can't see what your updated question has to do with the original one, but here's two ways to do what you wanted:

b = [a.dup, b.dup]
b[0][1] += 1.1
b[1][1] += 1.1

or

b.map! { |r| r.dup.tap { |r2| r2[1] += 1.1 } }
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • sorry, I didn't explain myself very well. This now messes with the x and a values, and I don't want it to do that. – Poyda Oct 30 '19 at 05:45
  • Ah, I didn't notice `a` and `x` were unchanged. Will change in a sec. – Amadan Oct 30 '19 at 05:46
  • Just to let you know I was using Ruby in Sketchup. Should I close this question because it *does* work in Ruby, or edit it? – Poyda Oct 30 '19 at 05:56
  • The second option works in Sketchup, however in both Ruby and Sketchup Ruby, I have the result ```3.3000000000000003``` if one of the values is ```3.3```? – Poyda Oct 31 '19 at 03:14
  • Yep, not Ruby's fault: check out [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). In Ruby you can use [other datatypes](https://ruby-doc.org/core-2.5.0/Rational.html) to overcome it; for example, `(1.1r + 2.2r).to_f` will give you `3.3`. – Amadan Oct 31 '19 at 03:19
  • as in change over the floats to rationals, then back again...right, thanks for that – Poyda Nov 04 '19 at 00:16