Calling functions in a child class

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

Brian Schlect wrote:

end
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.

Hi –

On Thu, 26 Oct 2006, Brian Schlect wrote:

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.

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

Phrogz wrote:

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”]

Brian Schlect wrote:

end
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”]

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

Brian Schlect wrote:

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”]