[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: [SOLUTION] The Solitaire Cipher (#1

James Gray

9/28/2004 1:24:00 AM

On Sep 26, 2004, at 9:24 AM, Florian Gross wrote:

> Moin!
>
> Here's my solution for the Solitaire Cipher quiz. It's fairly
> class-oriented and longish.

Help. <laughs> I'm trying to figure this code out...

> module Solitaire
> extend self

# snip module definition...

> end

I'm trying to understand the above trick:

extends self

would be the same as:

self.extends self

Which just leaves the question of what is self? Obviously, it has to
be the module itself. That means we added the methods to the module
itself?

That doesn't make sense alone; however, I know extending a module in
in a class definition adds the module methods as class methods. If I
assume the same is happening here...

My guess: You duplicated all of Solitaires' methods as class methods?

If I'm right, you could have done the same by declaring all those
methods as:

def Solitaire.<method_name>...

Right?

Hmm, wait. Not quite. Then you would be missing the instance methods.

So why did you write it this way?

Big Guess (really reaching now): To provide a convenient interface to
use the module stand-alone while also allowing it to be mixed into
future creations?

Thanks for the lesson.

James Edward Gray II



1 Answer

Florian Gross

9/28/2004 4:31:00 PM

0

James Edward Gray II wrote:

> I'm trying to understand the above trick:
>
> extends self
>
> My guess: You duplicated all of Solitaires' methods as class methods?
>
> [snip]
>
> So why did you write it this way?
>
> Big Guess (really reaching now): To provide a convenient interface to
> use the module stand-alone while also allowing it to be mixed into
> future creations?

Exactly. I think the same would be done if I had used a standalone
module_function(), but then all the instance methods would also be private.

It's interesting how Ruby's object model lets you do things like this.

> Thanks for the lesson.

Glad I could give it. :)

Regards,
Florian Gross