Can I get a method object for a class method?

Hi:

Is is possible to get a pointer to a class method as in the
code below:

class Demo
def initialize
@func1 = method( :somemethod )
@func2 = method( ??self.class_method?? ) # is there a syntax for this?
end

def call_methods
@func1.call #=> some method
@func2.call #=> would like this to be "class method"
end

def somemethod
puts "some method"
end

def self.class_method
puts "class method"
end

end

Demo.new.call_methods

···


Jim Freeze
If only I had something clever to say for my comment…
~

class A
def self.xyz
p “I’m xyz”
end
@@classMeth = method(:xyz)
def initialize
@f1 = @@classMeth
end
def test
@f1.call
end
end

A.new.test

···

On Sunday 04 August 2002 03:18 pm, Jim Freeze wrote:

Is is possible to get a pointer to a class method as in the
code below:


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

Hi,

···

At Mon, 5 Aug 2002 07:18:58 +0900, Jim Freeze wrote:

class Demo
def initialize
@func1 = method( :somemethod )
@func2 = method( ??self.class_method?? ) # is there a syntax for this?
@func2 = self.class.method(:class_method)


Nobu Nakada

Ned Konz wrote:

Is is possible to get a pointer to a class method as in the
code below:

class A
def self.xyz
p “I’m xyz”
end
@@classMeth = method(:xyz)
def initialize
@f1 = @@classMeth
end
def test
@f1.call
end
end

A.new.test

If you subclass A, this will bite you:

class B < A
def self.xyz
p “I’m xyz in B”
end
end

B.new.test # ==> “I’m xyz”

Instead:

class A
def initialize
@f1 = self.class.method(:xyz) # or use ‘type’ for ‘self.class’
end
end

B.new.test # ==> “I’m xyz in B”

···

On Sunday 04 August 2002 03:18 pm, Jim Freeze wrote: