Is there standard idiom to add module methods to classes that mixin
them? (Heh, is “mixin” also a verb in Ruby?)
That is, I would like class C to croak:
module M
def self.croak
puts “croak!”
end
end
class C
include M
croak
end
– fxn
Is there standard idiom to add module methods to classes that mixin
them? (Heh, is “mixin” also a verb in Ruby?)
That is, I would like class C to croak:
module M
def self.croak
puts “croak!”
end
end
class C
include M
croak
end
– fxn
On 2/21/06, Xavier N. [email protected] wrote:
class C
include M
croak
end
When you say you want class C to croak, I assume you really mean you’d
like for objects from the class C to be able to croak. Remove the line
“croak” from the class C definition and do this.
c = C.new
c.croak
The rest of your code looks fine.
Hi –
On Tue, 21 Feb 2006, Mark V. wrote:
end
c = C.new
c.croakThe rest of your code looks fine.
I don’t think that’s what Xavier wanted; I think he wanted C to be
able to call M.croak. (Also, in your example, c doesn’t respond to
croak.)
This is an often-discussed question. Some common answers:
And probably more I would give gold, silver, and bronze to #1,
#3, and #2, in that order.
David
–
David A. Black ([email protected])
Ruby Power and Light (http://www.rubypowerandlight.com)
“Ruby for Rails” chapters now available
from Manning Early Access Program! Ruby for Rails
On 2/21/06, [email protected] [email protected] wrote:
I don’t think that’s what Xavier wanted; I think he wanted C to be
able to call M.croak. (Also, in your example, c doesn’t respond to
croak.)
Wow! I have a fundamental misunderstanding of mixins apparently. I
see that you are correct. c doesn’t respond to croak in the following
code. However, I don’t understand why. Doesn’t the include add
instance methods from the module M to the class C?
module M
def self.croak
puts ‘croak’
end
end
class C
include M
end
c = C.new
c.croak
Hi –
On Wed, 22 Feb 2006, Mark V. wrote:
c = C.new
c.croak
Yes, but croak isn’t an instance method (Note the “self.” part.)
David
–
David A. Black ([email protected])
Ruby Power and Light (http://www.rubypowerandlight.com)
“Ruby for Rails” chapters now available
from Manning Early Access Program! Ruby for Rails
On Feb 21, 2006, at 8:31 AM, Xavier N. wrote:
class C
include M
croak
end
I tend to do it like this:
Module M
extend self # duplicate instance methods as class methods
def croak
puts “Croak!”
end
end
class C
extend M # class-level mixin
croak
end
Hope that helps.
James Edward G. II
On 2/21/06, [email protected] [email protected] wrote:
Wow! I have a fundamental misunderstanding of mixins apparently. I
class C
include M
endc = C.new
c.croakYes, but croak isn’t an instance method
(Note the “self.” part.)
Silly me! I briefly crossed things in my mind and thought that
including “self.” made it an instance method … and I thought I got
enough sleep last night.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs