I have this line, where the input variables in the array can come in any
form — string, integer, fixnum, float (ps, I know that sales_price as
a
currency should really be BigDecimal or the like on instantiation, this
is
just to illustrate my question):
[quantity, sales_price].each { |multiplicand| multiplicand =
BigDecimal.new(multiplicand.to_s) if multiplicand.class != BigDecimal}
So if
(rdb:1) quantity = 50
(rdb:1) quantity.class
Fixnum
(rdb:1) sales_price = 50
(rdb:1) sales_price.class
Float
And I call this:
(rdb:1) [quantity, sales_price].each { |multiplicand| multiplicand =
BigDecimal.new(multiplicand.to_s) if multiplicand.class != BigDecimal}
[50, 1.99]
Why don’t the variables change?
(rdb:1) quantity.class
Fixnum
(rdb:1) sales_price.class
Float
(rdb:1)
Because if I do this:
[quantity, sales_price].map { |multiplicand| multiplicand =
BigDecimal.new(multiplicand.to_s) if multiplicand.class != BigDecimal}
I do get:
[#BigDecimal:3b4f4f8,‘0.5E2’,4(8),
#BigDecimal:3b4f46c,‘0.199E1’,8(8)]
… so I know that the logic is working.
Shouldn’t an operation in an each change the input? — I thought the
input
should be by ref.