[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: "exporting" module methods

James Gray

2/21/2006 3:13:00 PM

On Feb 21, 2006, at 8:31 AM, Xavier Noria wrote:

> 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

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 Gray II