I tried to replace self.fixture_class_names = self.fixture_class_names.merge(class_names) with self.fixture_class_names.merge!(class_names) # Why does this fail? in rails source, and it failed 2 tests. What is the difference between the two? Could it have something to do with the fact that the fixture_class_names hash has a block to generate values that were not assigned? Or is this because fixture_class_names is a class attribute? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 December 2011 20:57, Alexey Muranov <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I tried to replace > > self.fixture_class_names = self.fixture_class_names.merge(class_names) > > with > > self.fixture_class_names.merge!(class_names) # Why does this fail?At a guess, I''d say because "self.fixture_class_names=" is a method that does some setting within it, while with the other syntax, you''re trying to call a bang method on the return value of a method. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 1, 9:16 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1 December 2011 20:57, Alexey Muranov <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > I tried to replace > > > self.fixture_class_names = self.fixture_class_names.merge(class_names) > > > with > > > self.fixture_class_names.merge!(class_names) # Why does this fail? > > At a guess, I''d say because "self.fixture_class_names=" is a method > that does some setting within it, while with the other syntax, you''re > trying to call a bang method on the return value of a method.Getting very warm! self.fixture_class_names is (or at least was - haven''t checked rails 3.1) a superclass_delegating_accessor. This means that when you call it on a subclass it travels up the inheritance hierarchy looking for a class where it has been set. So if we had classes A < B < C < D, and A.fixture_class_names = x then calling D.fixture_class_names checks D, then C, then B a lastly A where it stops, since a value has been defined for A. When you call C.fixture_class_names = y that doesn''t change what B does (it checks B and then A), but it changes what D does (it checks D, then C and returns y). However, if you do C.fixture_class_names.merge! (without having called C.fixture_class_names=) that will change the value that ''belongs'' to A, thus changing what A.fixture_class_names and what B.fixture_class_names returns 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #1034631:> On Dec 1, 9:16pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> At a guess, I''d say because "self.fixture_class_names=" is a method >> that does some setting within it, while with the other syntax, you''re >> trying to call a bang method on the return value of a method. > > Getting very warm! self.fixture_class_names is (or at least was - > haven''t checked rails 3.1) a superclass_delegating_accessor. > This means that when you call it on a subclass it travels up the > inheritance hierarchy looking for a class where it has been set. > So if we had classes A < B < C < D, and A.fixture_class_names = x then > calling D.fixture_class_names checks D, then C, then B a lastly A > where it stops, since a value has been defined for A. > When you call C.fixture_class_names = y that doesn''t change what B > does (it checks B and then A), but it changes what D does (it checks > D, then C and returns y). > However, if you do C.fixture_class_names.merge! (without having > called C.fixture_class_names=) that will change the value that > ''belongs'' to A, thus changing what A.fixture_class_names and what > B.fixture_class_names returns > > FredThanks for the explanation. fixture_class_names is in fact declared with class_attribute :fixture_class_names Didn''t you mean D < C < B < A ? Alexey. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 2, 12:42 pm, Alexey Muranov <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #1034631: > > > class_attribute :fixture_class_namesThe implementation is a bit different but the no-no on mutating the blah.fixture_class_names is the same> > Didn''t you mean D < C < B < A ? >yes :-) A simpler example in which merge! and merge= wouldn''t be the same would be class Foo def self.bar @bar.dup #prevent people mutating me! end def self.bar=(value) @bar = value end end Clearly Foo.bar.merge!(...) will not accomplish much here Fred> Alexey. > > -- > Posted viahttp://www.ruby-forum.com/.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.