[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling functions in a child class

Brian Schlect

10/26/2006 12:31:00 AM

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

--
Posted via http://www.ruby-....

6 Answers

Trans

10/26/2006 12:58:00 AM

0


Brian Schlect wrote:
> 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.

dblack

10/26/2006 2:02:00 AM

0

Gavin Kistner

10/26/2006 3:42:00 AM

0

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

Gavin Kistner

10/26/2006 3:56:00 AM

0

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

Joel VanderWerf

10/26/2006 5:07:00 AM

0

Brian Schlect wrote:
> 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"]


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Brian Schlect

10/26/2006 6:23:00 AM

0

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

--
Posted via http://www.ruby-....