Hello all,
I have another question regarding Mixins. How can I mix in class
methods?
I’d like the folling to work:
module Foo
def Foo.foo # this doesn’t work
return "foo"
end
end
class Bar
include Foo
end
Bar.foo # -> NameError: undefined method `foo’ for Bar:Class
Any help is appreciated!
-billy.
···
–
Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9
Try “extend” (see below):
···
module A
def b(x)
print x, "\n"
end
end
class Foo
extend A
end
Foo.b(3) ==> “3”
Steve Tuckner
-----Original Message-----
From: Philipp Meier [mailto:meier@meisterbohne.de]
Sent: Wednesday, July 03, 2002 1:26 PM
To: ruby-talk@ruby-lang.org
Subject: Mixin class methods
Hello all,
I have another question regarding Mixins. How can I mix in class
methods?
I’d like the folling to work:
module Foo
def Foo.foo # this doesn’t work
return "foo"
end
end
class Bar
include Foo
end
Bar.foo # -> NameError: undefined method `foo’ for Bar:Class
Any help is appreciated!
-billy.
Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9
Be forewarned that class variables used in A are A’s class variables,
and not Foo’s:
module A
def c=(x)
@@c = x
end
def c
@@c
end
end
class Foo
extend A
end
Foo.c = 3
p Foo.c #=> 3
class Bar
extend A
end
p Bar.c #=> 3
I’ve been bitten by this before.
Paul
···
On Thu, Jul 04, 2002 at 03:35:20AM +0900, Steve Tuckner wrote:
Try “extend” (see below):
module A
def b(x)
print x, “\n”
end
end
class Foo
extend A
end
Foo.b(3) ==> “3”