On Tue, Feb 8, 2011 at 9:06 AM, Frederick Cheung <[email protected]
> wrote:

>
> On Feb 8, 2:44 pm, David Kahn <[email protected]> wrote:
> > 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}
> >
> [snip]
>
> All each does is iterate over a collection, yielding to the block.
> when you do multiplicand = blah, you are just setting the local
> variable multiplicand to point at some new object - you're not
> mutating the actual object.
>

I see, I guess I got confused as I use ActiveRecord so much and within a
each block, by setting values and calling save it of course saves the
object. Is there a way to achieve what I am presenting, though, passing a
batch of variables and having them operated on and changed?

This situation:
(rdb:1) v1 = 1
1
(rdb:1) v2 = 2
2
(rdb:1) v3 = 3
3
(rdb:1) arr = [v1,v2,v3]
[1, 2, 3]
(rdb:1) arr = arr.map{|i| i = i + 1}
[2, 3, 4]

However what I would want is to see the variable values changed but they are
not:
(rdb:1) v1
1
(rdb:1) v2
2

Excuse my ingenuity, but I am trying to understand... if I have an array of
variables it seems that when the array is created, the elements which were
variables are substituted by values, and there is no reference:
(rdb:1) arr = [v1,v2,v3]
[1, 2, 3]
(rdb:1) v1
1
(rdb:1) arr[0]=5
5
(rdb:1) v1
1

This must sound like kindergarten but apparently I have been working albeit
successfully under some very false premises.



>
> Fred
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to