[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Class Methods from Modules

Greg Willits

11/22/2007 5:58:00 PM

Can't see the tree for the forest.

Playing with how to integrate modules, classes, subclasses, and create
instance methods and class methods from modules.

In this play code below, I want to use initialize_colors in either the
class Shape or Square as a class method.

At this point I'm sure I'm missing a very simple syntax rule explained
very clearly in the books, but the code as is complains about
initialize_colors as being undefined, and I'm just not recognizing what
I am missing.


module Color

def initialize_colors
@fill_color = 'ffffff'
@border_color = '000000'
end

def set_fill_rgb(color)
if color == 'white'
@fill_color = 'ffffff'
elsif color == 'red'
@fill_color = 'ff0000'
elsif color == 'blue'
@fill_color = '0000ff'
elsif color == 'black'
@fill_color = '000000'
end
end
end

class Shape

include Color

attr_reader :fill_color, :border_color

def initialize
@fill_color = String.new
@border_color = String.new
end
end

class Square < Shape

initialize_colors

end

sq = Square.new
puts sq.fill_color
sq.set_fill_rgb('red')
puts sq.fill_color
--
Posted via http://www.ruby-....

13 Answers

Eric I.

11/22/2007 6:39:00 PM

0

Hi Greg,

Well the methods you can call *as* a class is being defined are
*class* methods. initialize_colors is an instance method, as it
should be since it's setting instance variables.

It seems to me that you want to call initialize_colors not as the
class is being defined but as instances are being created. And
therefore the call to initialize_colors should be inside Square's
initialize method.

Eric

====

Are you interested in on-site Ruby training that's been highly
reviewed by former students? http://Lea...

On Nov 22, 12:58 pm, Greg Willits <li...@gregwillits.ws> wrote:
> Can't see the tree for the forest.
>
> Playing with how to integrate modules, classes, subclasses, and create
> instance methods and class methods from modules.
>
> In this play code below, I want to use initialize_colors in either the
> class Shape or Square as a class method.
>
> At this point I'm sure I'm missing a very simple syntax rule explained
> very clearly in the books, but the code as is complains about
> initialize_colors as being undefined, and I'm just not recognizing what
> I am missing.
>
> module Color
>
> def initialize_colors
> @fill_color = 'ffffff'
> @border_color = '000000'
> end
>
> def set_fill_rgb(color)
> if color == 'white'
> @fill_color = 'ffffff'
> elsif color == 'red'
> @fill_color = 'ff0000'
> elsif color == 'blue'
> @fill_color = '0000ff'
> elsif color == 'black'
> @fill_color = '000000'
> end
> end
> end
>
> class Shape
>
> include Color
>
> attr_reader :fill_color, :border_color
>
> def initialize
> @fill_color = String.new
> @border_color = String.new
> end
> end
>
> class Square < Shape
>
> initialize_colors
>
> end
>
> sq = Square.new
> puts sq.fill_color
> sq.set_fill_rgb('red')
> puts sq.fill_color
> --
> Posted viahttp://www.ruby-....

Greg Willits

11/22/2007 6:58:00 PM

0

Eric I. wrote:
> Hi Greg,
>
> Well the methods you can call *as* a class is being defined are
> *class* methods. initialize_colors is an instance method, as it
> should be since it's setting instance variables.
>
> It seems to me that you want to call initialize_colors not as the
> class is being defined but as instances are being created. And
> therefore the call to initialize_colors should be inside Square's
> initialize method.


The exercise is to figure how to make initialize_colors work as a class
method -- so if the instance vars are misleading, then it's those I
would change.

I am just experiementing to understand with working examples how various
syntax and behavior are related in Ruby to acheive various levels of
functionality at the class, instance, and module realms.

Another way to look at this is that I'm trying to understand how some
things in Rails work by replicating them on a micro scale -- in this
case a good example might be how the validates_ methods all work.
They're called as class methods from within a subclass and I would
expect they're implemented via modules. I've traced the source, but
there's too much noise for me at this stage of my Ruby experience.

-- gw

--
Posted via http://www.ruby-....

Greg Willits

11/22/2007 7:46:00 PM

0

I found class << self, but still here's an even simpler example that
doesn't work as I thought it would:

module Color
def set_fill(color)
puts "testing"
@@fill_color = color
end
end

class Shape

@@fill_color = 'black'

class << self
include Color
end

def self.fill_color
@@fill_color
end

end

Shape.new
puts Shape.fill_color

Shape.set_fill('red')
puts Shape.fill_color

I get:

black
testing
black

--gw

--
Posted via http://www.ruby-....

Phrogz

11/23/2007 3:19:00 AM

0

Greg Willits wrote:
> In this play code below, I want to use initialize_colors in either the
> class Shape or Square as a class method.

Including a module in a class causes instance methods of the module to
be in the lookup path for instances of the class. Extending a class
using a module causes instance methods of the module to be in the
lookup path for the class itself.

(See http://phrogz.net/RubyLibs/RubyMethodLook...)

A common idiom to be able to cause a class to get both class and
instance methods when including a single module follows:

module Color
module ClassMethods
def initialize_colors
p "woot!"
end
end

def giggle
puts "tee hee!"
end

def self.included(receiver)
receiver.extend ClassMethods
end
end

class Shape
include Color
initialize_colors
#=> "woot!"
end

Shape.new.giggle
#=> "tee hee!"

Greg Willits

11/23/2007 3:46:00 AM

0

Gavin Kistner wrote:
> Greg Willits wrote:
>> In this play code below, I want to use initialize_colors in either the
>> class Shape or Square as a class method.
>
> Including a module in a class causes instance methods of the module to
> be in the lookup path for instances of the class. Extending a class
> using a module causes instance methods of the module to be in the
> lookup path for the class itself.
>
> (See http://phrogz.net/RubyLibs/RubyMethodLook...)
>
> A common idiom to be able to cause a class to get both class and
> instance methods when including a single module follows:

Cool, thanks. But...

In this code the fill_color is still never set to black. It seems to me
that Module code even if added as class methods cannot access class
variables??

-- gw

module Color
module ClassMethods
def initialize_colors
@@fill_color = 'black'
p "woot!"
end
end

def self.included(receiver)
receiver.extend ClassMethods
end
end

class Shape
include Color
@@fill_color = 'white'
initialize_colors

def self.fill_color
@@fill_color
end
end

Shape.new
p Shape.fill_color
--
Posted via http://www.ruby-....

Greg Willits

11/23/2007 4:55:00 AM

0

Greg Willits wrote:
> Gavin Kistner wrote:
>> Greg Willits wrote:
>>> In this play code below, I want to use initialize_colors in either the
>>> class Shape or Square as a class method.
>>
>> Including a module in a class causes instance methods of the module to
>> be in the lookup path for instances of the class. Extending a class
>> using a module causes instance methods of the module to be in the
>> lookup path for the class itself.
>>
>> (See http://phrogz.net/RubyLibs/RubyMethodLook...)
>>
>> A common idiom to be able to cause a class to get both class and
>> instance methods when including a single module follows:
>
> Cool, thanks. But...
>
> In this code the fill_color is still never set to black. It seems to me
> that Module code even if added as class methods cannot access class
> variables?


OK, nevermind -- I get it now.

even though they have the same name they are actually two independent
variables in the two separate "name spaces/realms" of the model & class
containers.

I thought the "inclusion" would fuse the two spaces into one.

-- gw
--
Posted via http://www.ruby-....

Ed Stasiak

11/2/2013 12:33:00 PM

0

> BTR1701
>
> And the fake name Kung-Fu Uncle is using amongst the townsfolk

I'm wondering how it's even possible that the Evil FedGov doesn't
know who Kung-Fu Uncle is and didn't sentenced him to death right
alongside Evil Monroe?

He was the second most well known figure in the Monroe Rep. and
Evil Monroe's Vice-Dictator and in charge of Operations, training and
leading the post-electropocalyptic Einsatzgruppen in oppressing the
peasants.

Have the writers somehow forgotten this?

Thanatos

11/2/2013 5:24:00 PM

0

In article <q08fkaxcqa.ln2@news.ezprovider.com>,
"Ed Stasiak" <a57160b@webnntp.invalid> wrote:

> > BTR1701
> >
> > And the fake name Kung-Fu Uncle is using amongst the townsfolk
>
> I'm wondering how it's even possible that the Evil FedGov doesn't
> know who Kung-Fu Uncle is and didn't sentenced him to death right
> alongside Evil Monroe?
>
> He was the second most well known figure in the Monroe Rep. and
> Evil Monroe's Vice-Dictator and in charge of Operations, training and
> leading the post-electropocalyptic Einsatzgruppen in oppressing the
> peasants.
>
> Have the writers somehow forgotten this?

No, they even brought it up in the episode. Monroe told him, "If they
know who I am, don't you think they know who you are, too?"

al2048

11/3/2013 7:52:00 AM

0

On Saturday, November 2, 2013 6:57:57 AM UTC-6, Caprice wrote:
> On Sat, 02 Nov 2013 05:32:58 -0700, Ed Stasiak wrote:
>
>
>
> > Have the writers somehow forgotten this?
>
>
>
> You *do* remember which show is being discussed in this thread, don't
>
> you? ;)


What is weird is that, even though Miles was well-known as Monroe's #2, Charlie did not seem to know that in the first episode of the show, when she found Miles in Chicago.

Hunter

11/3/2013 9:48:00 AM

0

In article <l4t0cn$no$1@speranza.aioe.org>, Not.My@Real.Address.com says...
>
> Ed Stasiak wrote:
>
> >
> > Did anybody really think that Lost Juliet would _not_ slip Kinda Evil
> > Monroe a knock-out drug to fool the Evil FedGov into believing he
> > was dead? (If so, the "next week on" clip confirms that he's alive)
>
>
> No, I figured it had to be. But he wasn't breathing (I doubt there
> really is such a thing as really, really shallow breathing that her
> father wouldn't have noticed) and his heart was stopped. Unlike other
> shows that have done this (Nikita and Covert Affairs in recent years),
> in which they've had to revive the person in three minutes or so, this
> time it's been a couple of hours. Doesn't seem possible to me, unless
> the nannites are somehow involved. And why didn't she tell Miles and get
> him to to help her dig?
>
> --Robin
----
Actually there is. In the past before wide spread embalming of corpses, there
have been cases of people being buried alive because their breathing and heart
rate was so slow as to be nigh undetectable by the methods used at the time.
There have been cases today, not buried alive but people waking up in the
morgue. I am sure there are drugs that can simulate that in real life so Monroe
didn't have to be clinically dead for anytime, just give him a drug that would
slow his heart and respiration rates waaaay down, so yes Monroe was alive
between the time the drug was administered and Rachel digging him up
reluctantly.
--
----->Hunter

"No man in the wrong can stand up against
a fellow that's in the right and keeps on acomin'."

-----William J. McDonald
Captain, Texas Rangers from 1891 to 1907