Hi All, Today, when I debug, I found that I don''t understand a very basic function "super". <code> class User < ActiveRecord::Base def self.columns puts "here....................." @ret = super return @ret end end </code> This return only one line of "here.....................". It is very fine. However, when I called My codes are: <code> class User < ActiveRecord::Base def self.columns puts "here....................." super end end </code> The return is what I expected - a list of columns in User, but this display 18 lines of "here....................." in the console (the length of "columns" of my User is 17. What is the difference between the two super call? Thanks much! Arthur -- Posted via http://www.ruby-forum.com/.
Bjørn Arild Mæland
2009-May-08 18:42 UTC
Re: I don''t know "super".... Sorry for my ignorance
> Hi All, > > Today, when I debug, I found that I don''t understand a very basic > function "super". > > <code> > > class User < ActiveRecord::Base > def self.columns > puts "here....................." > @ret = super > return @ret > end > > end > </code> > > This return only one line of "here.....................". It is very > fine. However, when I called > > My codes are: > <code> > > class User < ActiveRecord::Base > def self.columns > puts "here....................." > super > end > > end > </code> > > The return is what I expected - a list of columns in User, but this > display 18 lines of "here....................." in the console (the > length of "columns" of my User is 17. > > What is the difference between the two super call?These two methods should both print the line "here....................." to standard output before they return an array of column objects. I''m not sure why you''re getting different results. Note that the super keyword has different semantics in Ruby than for example Java -- I''d suggest consulting the Pickaxe or Ruby for Rails if you''re confused about its behaviour.
Hi Bjørn, thanks for your direction. The question is created by one bug in my program - I wanna use the super to in my overriding "find_by_email" functions. I found that I can override it the first time, but afterwards, the super class method is called instead (skipped my overriding code totally). Now, I solved my original problem and bet that the "find_by_email" should be method_missing call, and the Rails remember the super class call afterwards. Is my estimation is correct? In anyway, I still cannot answer the 1 time output and 18 times outputs difference, I am after the 2nd methods indeed was triggered 18times. Thanks! Bjørn Arild Mæland wrote:>> @ret = super >> <code> >> The return is what I expected - a list of columns in User, but this >> display 18 lines of "here....................." in the console (the >> length of "columns" of my User is 17. >> >> What is the difference between the two super call? > > These two methods should both print the line > "here....................." to standard output before they return an > array of column objects. I''m not sure why you''re getting different > results. Note that the super keyword has different semantics in Ruby > than for example Java -- I''d suggest consulting the Pickaxe or Ruby > for Rails if you''re confused about its behaviour.-- Posted via http://www.ruby-forum.com/.