Forum: Ruby on Rails I don't know "super".... Sorry for my ignorance

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Arthur C. (Guest)
on 2009-05-08 15:06
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
Bjørn Arild Mæland (Guest)
on 2009-05-08 22:43
(Received via mailing list)
>    @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.
Arthur C. (Guest)
on 2009-05-10 08:43
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.
This topic is locked and can not be replied to.