[Q] how to detect if #super exists?

C

Chuck Remes

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
 
B

Brian Candler

Chuck said:
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)
 
R

Robert Klemme

2008/10/27 Pit Capitain said:
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
 
A

ara.t.howard

:)

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


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/
 
R

Robert Klemme

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

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

Cheers

robert
 
C

Chuck Remes

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)

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
 
B

Brian Candler

Chuck said:
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 :)
 
P

Pit Capitain

2008/10/27 Robert Klemme said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top