Phil
On Tue, Feb 8, 2011 at 11:26 AM, David Kahn <[email protected]>wrote: > > > On Tue, Feb 8, 2011 at 10:34 AM, Frederick Cheung < > [email protected]> wrote: > >> On Feb 8, 3:33 pm, David Kahn <[email protected]> wrote: >> > On Tue, Feb 8, 2011 at 9:06 AM, Frederick Cheung < >> [email protected] >> > >> > 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? >> >> that is very different. doing some_object.foo = 'bar' is calling a >> method on an object that changes its state, whereas >> foo = bar just points a local variable at something new. (and in >> addition Fixnums/BigDecimal are immutable - there is no method you can >> call on a number to change it into another nmber) >> > > I think I am starting to see, maybe. Hopefully not being too annoying here. > > > It seems rather strange to me that if you have an object, i.e. a string > that it should behave differently than my Account object.... the string#=() > method is still a property, right? -- no different really than > account#name=(), so seems it would be logical that if I can do > account.name = 'x' within an iterator, that doing string='y' should also > work, but as we see it does not (lets put the Fixnums and BigDecimal aside > as I understand as a property they are re-created rather than modified). > > arr.each {|account| account.name = 'x'} > > arr.each {|string| string=('y')} > > .... they seem to me to be the same thing, unless objects are really not > the same. > > > But with another experiment I see something interesting --- if I > instantiate a string formally as an object, then I do get the by reference > relationship in an array: > > ruby-1.9.2-p136 :098 > str = String.new('David') > => "David" > ruby-1.9.2-p136 :099 > arr = [str] > => ["David"] > ruby-1.9.2-p136 :100 > arr.each {|s| str = 'new val'} > => ["David"] > ruby-1.9.2-p136 :101 > str > => "new val" > > But if I just do it as a normal assignment, I do not: > > ruby-1.9.2-p136 :106 > string = 'David' > => "David" > ruby-1.9.2-p136 :107 > arr = [string] > => ["David"] > ruby-1.9.2-p136 :108 > arr.each {|s| s = 'new val'} > => ["David"] > ruby-1.9.2-p136 :109 > string > => "David" > > In the first case, you explicitly changed the value of the local variable str... it didn't change the array contents. In the second case you only changed the value inside the scope of the block that was enumerating through. Whether you created the string with String.new or not won't make a difference. Ruby does pass object by reference, but when you do s = "whatever" in the block, you're creating a new local variable that only lives as long as the scope of the block. If you want to modify an array of strings, for example, you could do: arr = ["John", "Doe"] => ["John","Doe"] arr.each{|s| s.replace("blah") } => ["blah","blah"] s.replace actually modifies the object, instead of creating a new one. > I thought that doing > > str = 'David' > > was just a shortcut to doing > > str = String.new('David') > > But apparently the behavior at least in the context of an array is > different. Wow! > > > >> >> > >> > 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] >> >> Without resorting to dirty tricks, you can't change the values of >> local variables other than by actually doing >> v1 = 2. arr does NOT contain [v1,v2,v3]. It contains 3 objects, and >> the local variables v1,v2,v3 just happen to also be referencing those >> same objects. >> >> Fred >> >> > (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. >> >> > -- > 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.

