[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

creating/accessing class vars from modules being used as mixins?

Christopher J. Bottaro

2/11/2007 10:26:00 PM

Hello,
I'm writing a module that is to be used as a mixin for classes. I
cannot create or access class variables from within module methods.
Please look at this small pastie. It contains the module definitions,
class definitions, and some sample code that demonstrates the errors
I'm receiving.

http://pastie.cabo...

Thanks for the help,
-- Christopher


3 Answers

James Gray

2/11/2007 11:16:00 PM

0

On Feb 11, 2007, at 4:26 PM, Christopher J. Bottaro wrote:

> I'm writing a module that is to be used as a mixin for classes. I
> cannot create or access class variables from within module methods.
> Please look at this small pastie. It contains the module definitions,
> class definitions, and some sample code that demonstrates the errors
> I'm receiving.

Class variables have confusing inheritance rules, that I don't think
are worth learning. (They are changing in a future version of Ruby.)

Instead, it's almost always easier to use instance variables on the
class/module itself. Your code could do this with changes like:

module MyModule

def self.included base
base.extend ForBase
end # def self.included

module ForBase

def message
@message
end

def message=(message)
@message = message
end

def acts_as_whatever options = {}
include MyModule::InstanceMethods
extend MyModule::ClassMethods

if options.has_key? :message
self.message = options[:message]
else
self.message = "no message"
end
end # def acts_as_whatever

end # module ForBase

module InstanceMethods

def show_message
puts self.class.message
end

end # module InstanceMethods

module ClassMethods

def print_message
puts message
end

end # module ClassMethods

end # module MyModule

James Edward Gray II

Christopher J. Bottaro

2/11/2007 11:28:00 PM

0

Wow, thanks for the fast response. Your solution worked perfectly.
On a side note, this should definitely be in a FAQ somewhere (if it
isn't already)... it's kind of a hard problem to figure out if you
don't know not to look at class variables.

Thanks again,
-- Christopher

On Feb 11, 5:16 pm, James Edward Gray II <j...@grayproductions.net>
wrote:
> On Feb 11, 2007, at 4:26 PM, Christopher J. Bottaro wrote:
>
> > I'm writing a module that is to be used as a mixin for classes. I
> > cannot create or access class variables from within module methods.
> > Please look at this small pastie. It contains the module definitions,
> > class definitions, and some sample code that demonstrates the errors
> > I'm receiving.
>
> Class variables have confusing inheritance rules, that I don't think
> are worth learning. (They are changing in a future version of Ruby.)
>
> Instead, it's almost always easier to use instance variables on the
> class/module itself. Your code could do this with changes like:
>
> module MyModule
>
> def self.included base
> base.extend ForBase
> end # def self.included
>
> module ForBase
>
> def message
> @message
> end
>
> def message=(message)
> @message = message
> end
>
> def acts_as_whatever options = {}
> include MyModule::InstanceMethods
> extend MyModule::ClassMethods
>
> if options.has_key? :message
> self.message = options[:message]
> else
> self.message = "no message"
> end
> end # def acts_as_whatever
>
> end # module ForBase
>
> module InstanceMethods
>
> def show_message
> puts self.class.message
> end
>
> end # module InstanceMethods
>
> module ClassMethods
>
> def print_message
> puts message
> end
>
> end # module ClassMethods
>
> end # module MyModule
>
> James Edward Gray II


The Peeler

2/10/2013 11:27:00 AM

0

On Sun, 10 Feb 2013 01:42:38 -0500, "All Bark & No Bite", aka Humpin
Humpton, aka Caroloony Reb, the subnormal moron and attested schizo from
the States, wrote:

>>> >> > A gook getting executed! Awesome indeed!
>
> Yes, indeed. I am not sure what kind of music would emit
> from a roasting gook in the Bull. However, there is sure
> to be yellow smoke.

Time for you to "sing" inside the padded cell again, schizoboy! LOL

--
Bert to our resident schizo:
"The voices in your head are not a reliable source of information."
MID: <XnsA0E69C0A32C1DVeebleFetzer@216.250.188.141>