[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

Mark Volkmann

2/21/2006 2:44:00 PM

On 2/21/06, Xavier Noria <fxn@hashref.com> 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

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.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


3 Answers

dblack

2/21/2006 2:59:00 PM

0

dblack

2/21/2006 3:39:00 PM

0

Mark Volkmann

2/21/2006 3:51:00 PM

0

On 2/21/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Wed, 22 Feb 2006, Mark Volkmann wrote:
>
> > On 2/21/06, dblack@wobblini.net <dblack@wobblini.net> 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
>
> Yes, 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. ;-)

--
R. Mark Volkmann
Partner, Object Computing, Inc.