Calling functions in a child class

B

Brian Schlect

I have a class:

class alpha
def test_method
puts "test method"
end

def get_methods
puts alpha.methods
end
end

running "get_methods" here will return a long list of methods including
test_method. But, I also have this class:

class bravo < alpha
def another_test_method
end
end

i want to be able to call get_methods in the parent class and have it
return the functions of the child class as well. so in this example:

test_obj = bravo.new
bravo.get_methods

would return all methods including

test_method
another_test_method

any ideas?

thanks,
Brian
 
T

Trans

Brian said:
I have a class:

class alpha
def test_method
puts "test method"
end

def get_methods
puts alpha.methods
end
end

running "get_methods" here will return a long list of methods including
test_method. But, I also have this class:

class bravo < alpha
def another_test_method
end
end

i want to be able to call get_methods in the parent class and have it
return the functions of the child class as well. so in this example:

test_obj = bravo.new
bravo.get_methods

would return all methods including

test_method
another_test_method

any ideas?

That's an expensive operation. Something along the lines of:

ObjectSpace.each(alpha) { |k|
p k.get_methods
}

In fact, that's so exepnsive you may want to reconsider what you're
trying to do here.

T.
 
D

dblack

Hi --

I have a class:

class alpha
def test_method
puts "test method"
end

def get_methods
puts alpha.methods
end
end

It's a little unclear what you're doing here. "class alpha" won't
parse (alpha isn't a constant), and since test_method is not defined
as a class method, it won't appear when you do alpha.methods.
running "get_methods" here will return a long list of methods including
test_method. But, I also have this class:

class bravo < alpha
def another_test_method
end
end

i want to be able to call get_methods in the parent class and have it
return the functions of the child class as well. so in this example:

test_obj = bravo.new
bravo.get_methods

would return all methods including

test_method
another_test_method

See if this does what you need:

class Alpha
def test_method
end

def get_methods
p self.class.instance_methods.sort # for easy viewing :)
end
end

Alpha.new.get_methods

class Bravo < Alpha
def another_test_method
end
end

Bravo.new.get_methods


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
P

Phrogz

Brian said:
i want to be able to call get_methods in the parent class and have it
return the functions of the child class as well. so in this example:

test_obj = bravo.new
bravo.get_methods

I'm confused. Do you want Alpha.get_methods to return Alpha's and
Bravo's methods, and Bravo.get_methods to return just Bravo's methods?

Or do you want the exact same list of methods returned regardless of
what level you call the method at?

Here's a solution for the former (and, because I wonder if it's
possibly what you want, it only includes the instance methods defined
by Alpha and Bravo):

class Alpha
def self.inherited( subklass )
@self_and_subklasses ||= [self]
@self_and_subklasses << subklass
end

def self.all_methods
@self_and_subklasses ||= [self]
@self_and_subklasses.map{ |klass|
klass.instance_methods( false )
}.flatten
end

def alpha_test1; end
def alpha_test2; end
end

class Bravo < Alpha
def bravo_test1; end
end

p Alpha.all_methods
#=> ["alpha_test1", "alpha_test2", "bravo_test1"]

p Bravo.all_methods
#=> ["bravo_test1"]
 
P

Phrogz

Phrogz said:
Or do you want the exact same list of methods returned regardless of
what level you call the method at?

Here's an example showing this end result. (Look ma! A real use for
class variables!)

class Alpha
@@alpha_and_subs = [ self ]
def self.inherited( subklass )
@@alpha_and_subs << subklass
end

def self.all_methods
@@alpha_and_subs.map{ |klass|
klass.instance_methods( false )
}.flatten
end

def alpha1; end
def alpha2; end
end

class Bravo < Alpha
def bravo1; end
end

class Charlie < Bravo
def charlie1; end
end

class Bingo < Alpha
def bingo1; end
end

p Alpha.all_methods
#=> ["alpha1", "alpha2", "bravo1", "charlie1", "bingo1"]

p Bravo.all_methods
#=> ["alpha1", "alpha2", "bravo1", "charlie1", "bingo1"]

p Charlie.all_methods
#=> ["alpha1", "alpha2", "bravo1", "charlie1", "bingo1"]
 
J

Joel VanderWerf

Brian said:
I have a class:

class alpha
def test_method
puts "test method"
end

def get_methods
puts alpha.methods
end
end

running "get_methods" here will return a long list of methods including
test_method. But, I also have this class:

class bravo < alpha
def another_test_method
end
end

i want to be able to call get_methods in the parent class and have it
return the functions of the child class as well. so in this example:

test_obj = bravo.new
bravo.get_methods

Did you mean this?

test_obj.get_methods

Then a simple solution is:

class Alpha
def test_method
puts "test method"
end

def get_methods(include_methods_inherited_by_alpha = false)
if include_methods_inherited_by_alpha
methods
else
methods - Alpha.superclass.instance_methods
end
end
end

alpha = Alpha.new
p alpha.get_methods
#p alpha.get_methods(true)
puts

class Bravo < Alpha
def another_test_method
end
end

bravo = Bravo.new
p bravo.get_methods
#p bravo.get_methods(true)

__END__

Output:

["get_methods", "test_method"]

["get_methods", "test_method", "another_test_method"]
 
B

Brian Schlect

That is what I was trying to do. Thanks alot. I mistyped my code
example, but you were able to read through it. Thank you, Joel
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top