How to detect if #super exists?

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

Chuck R. 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)

2008/10/27 Chuck R. [email protected]:

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

defined? super

Regards,
Pit

2008/10/27 Pit C. [email protected]:

2008/10/27 Chuck R. [email protected]:

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

defined? super

:slight_smile:

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

On 27.10.2008 17:29, ara.t.howard wrote:

On Oct 27, 2008, at 10:15 AM, Robert K. 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

On Oct 27, 2008, at 10:15 AM, Robert K. 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/

Chuck R. wrote:

On Oct 27, 2008, at 10:33 AM, Brian C. 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 :slight_smile:

2008/10/27 Robert K. [email protected]:

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

On Oct 27, 2008, at 10:33 AM, Brian C. 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