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…
~
Ned_Konz
(Ned Konz)
4 August 2002 22:28
2
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
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: