From: Logan Capaldo Date: 2006-05-03T01:51:05+09:00 Subject: Re: Mixin Syntax for Newbies On May 2, 2006, at 12:40 PM, Nathan Olberding wrote: > I'm trying to start using mixins and I'm having a little syntactual > trouble, if that's a word. > > Here's my test code: > > --------------- > module One > @one = "One!" > attr_reader :one > end > > class Two > include One > def initialize > puts @one > end > end > > this = Two.new > ---------------- > > I have tried several variations on this, but Two.one always seems to > come out as "nil". Is it possible to set it to "One!" by default? First of all when you say module One @one = "One!" end That sets the instance variable of the module One. Second, this: def initialize puts @one end defeats the whole purpose of using attr_reader Finally, you may want to do something like: module One def one @one ||= "One!" # sets @one to "One!" only if it's not already set # or is nil or false end end class Two include One def initialize puts one end end