Forum: Ruby how to detect if #super exists?

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.
A0c079a7c3c9b2cf0bffebd84dc578b0?d=identicon&s=25 Chuck Remes (cremes)
on 2008-10-27 16:03
(Received via mailing list)
I'm writing some modules that I use to replace/extend the
functionality in a base class. I want to test these modules without
adding them to the original class.

My problem is that some of the module methods call #super to preserve
the base class' functionality. However, in the spec there is no base
class and therefore no #super so I get a NoMethodError.

What is the proper way to check for the existence of a #super method
before invoking it?

cr
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-27 16:34
Chuck Remes wrote:
> What is the proper way to check for the existence of a #super method
> before invoking it?

Hmm, how are you testing instance methods of a module without actually
mixing them into an object?

Maybe it would be easier just to have a fake superclass for testing,
with stubs for those methods. (Indeed, then you can test that super *is*
actually being called)
50b2daf0e7666574579b9edaf8f2b69a?d=identicon&s=25 Pit Capitain (Guest)
on 2008-10-27 16:46
(Received via mailing list)
2008/10/27 Chuck Remes <cremes.devlist@mac.com>:
> What is the proper way to check for the existence of a #super method before
> invoking it?

defined? super

Regards,
Pit
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-27 17:16
(Received via mailing list)
2008/10/27 Pit Capitain <pit.capitain@gmail.com>:
> 2008/10/27 Chuck Remes <cremes.devlist@mac.com>:
>> What is the proper way to check for the existence of a #super method before
>> invoking it?
>
> defined? super

:-)

I believe OP rather wanted to test for existence of the same method in
the superclass.  Although I have to say I am not 100% sure what he's
after since the testing without instance seems strange.

An alternative would be to invoke it and rescue NoMethodError.
Another route would be to use instance_method but that might be
fragile.

Kind regards

robert
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 Ara Howard (ahoward)
on 2008-10-27 17:30
(Received via mailing list)
On Oct 27, 2008, at 10:15 AM, Robert Klemme wrote:

> Kind regards
>
> robert


precisely what 'defined?(super)' does....

cfp:~ > cat a.rb
class A
   def foo() end
end

class B < A
   def foo() defined?(super) ? true : false end
end

class C
   def foo() defined?(super) ? true : false end
end

p B.new.foo
p C.new.foo



cfp:~ > ruby a.rb
true
false


cheers.

a @ http://codeforpeople.com/
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-27 19:04
(Received via mailing list)
On 27.10.2008 17:29, ara.t.howard wrote:
> On Oct 27, 2008, at 10:15 AM, Robert Klemme wrote:
>
>> I believe OP rather wanted to test for existence of the same method in
>> the superclass.  Although I have to say I am not 100% sure what he's
>> after since the testing without instance seems strange.
>
> precisely what 'defined?(super)' does....

Amazing!  Learn something new every day.  Thanks, Ara, much appreciated.
  Sorry for the noise, Pit.

Cheers

  robert
A0c079a7c3c9b2cf0bffebd84dc578b0?d=identicon&s=25 Chuck Remes (cremes)
on 2008-10-27 20:11
(Received via mailing list)
On Oct 27, 2008, at 10:33 AM, Brian Candler wrote:

> actually being called)
My specs have an anonymous class that I create in the #before block.

e.g.

before(:each) do
   @base = Class.new do
      # set instance variables, etc.
     end.new
   @base.extend MyModule
end

it "should call #foo" do
   ...
end

Looks like #defined?(super) was what I needed.

Thanks for the help.

cr
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-27 22:21
Chuck Remes wrote:
> On Oct 27, 2008, at 10:33 AM, Brian Candler wrote:
>
>> actually being called)
> My specs have an anonymous class that I create in the #before block.
>
> e.g.
>
> before(:each) do
>    @base = Class.new do
>       # set instance variables, etc.
>      end.new
>    @base.extend MyModule
> end

Ah, so perhaps you could 'stub' these methods as

  @base = Class.new do
    def foo; end
    end.new

> Looks like #defined?(super) was what I needed.

That was new to me too :-)
50b2daf0e7666574579b9edaf8f2b69a?d=identicon&s=25 Pit Capitain (Guest)
on 2008-10-28 00:18
(Received via mailing list)
2008/10/27 Robert Klemme <shortcutter@googlemail.com>:
> Amazing!  Learn something new every day.  Thanks, Ara, much appreciated.
> Sorry for the noise, Pit.

No problem, Robert. I think I learned this here on ruby-talk, too. And
thanks Ara for the code. I didn't have much time when I sent my first
post.

Regards,
Pit
This topic is locked and can not be replied to.