From: Robert Klemme Date: 2006-01-11T18:53:06+09:00 Subject: Re: included module and scope confusion ------=_NextPart_000_001E_01C6169C.8B417570 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit hmmm wrote: > I have tried different ways, but can't get the Correct Way to have a > module insert a variable into the Class that includes it. In this > case it should inject a @cookies hash, but in tests @cookies always > gets reset to Hash.new... Of course because you assign it in initialize. Two options: 1 Lazy init: You need to define cookies differently and access it always through the getter method (attachment ex1.rb) 2 Init during constructor: you need to define #initialize in the module and either leave initialize out of Bread or use super (attachment ex2.rb) Module#included is completely wrong here as you do not want to do anything to the class that uses the mod. > Excuse the inconsistencies in the code (towards @cookies) b/c I can't > seem to figure out the proper way. > > So how do you add instance vars to a class from a module and how to > reference that attribute? Or is this not the Right Ruby Way? > > I have a lot of confusion about modules... suppose its my java funk :/ Apparently. :-) > Please point me to any more docs on modules besides the ruby-lang > snippet. HTH Kind regards robert ------=_NextPart_000_001E_01C6169C.8B417570 Content-Type: application/octet-stream; name="ex1.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="ex1.rb" module Crumbs=0A= MAX_CRUMBS =3D 10=0A= =0A= def cookies=0A= # lazy init=0A= @cookies ||=3D {}=0A= end=0A= =0A= def crumb_new( controller, action, params )=0A= cstr =3D controller + "^" + action=0A= if params !=3D nil && params.keys !=3D nil=0A= params.keys.each {|p|=0A= if params[p] !=3D nil=0A= cstr << "^" << p << "^" << params[p]=0A= end=0A= }=0A= end=0A= return cstr=0A= end=0A= =0A= def crumb_add( bite )=0A= cbs =3D cookies=0A= puts cbs.class=0A= cbs =3D cbs[:rdf_crumbs]=0A= if cbs =3D=3D nil=0A= puts "crumb_add::cbs is nil!"=0A= cbs =3D[bite]=0A= else=0A= puts "crumb_add::cbs is OK!"=0A= cbs =3D cbs.split("|")=0A= #don't add a redundant=0A= if cbs.last =3D=3D bite=0A= return=0A= end=0A= # add to=0A= cbs << bite=0A= # cap the cookie queue at 5 using fifo=0A= if cbs.length > @@MAX_CRUMBS=0A= cbs.delete_at(0)=0A= end=0A= end=0A= crumbs_set( cbs )=0A= end=0A= =0A= def crumbs_set( mouthful )=0A= cr =3D ""=0A= mouthful.each { |m|=0A= if cr =3D=3D ""=0A= cr =3D m=0A= else=0A= cr << "|" << m=0A= end=0A= }=0A= puts cookies.class=0A= cookies[:rdf_crumbs] =3D cr=0A= end=0A= =0A= def get_crumbs()=0A= crs =3D cookies["rdf_crumbs"]=0A= if crs =3D=3D nil=0A= return []=0A= else=0A= return crs.split("|")=0A= end=0A= end=0A= =0A= end=0A= =0A= =0A= class Bread=0A= include Crumbs=0A= end=0A= ------=_NextPart_000_001E_01C6169C.8B417570 Content-Type: application/octet-stream; name="ex2.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="ex2.rb" module Crumbs=0A= MAX_CRUMBS =3D 10=0A= =0A= attr_reader :cookies=0A= =0A= def initialize(*a,&b)=0A= super=0A= @cookies =3D {}=0A= end=0A= =0A= def crumb_new( controller, action, params )=0A= cstr =3D controller + "^" + action=0A= if params !=3D nil && params.keys !=3D nil=0A= params.keys.each {|p|=0A= if params[p] !=3D nil=0A= cstr << "^" << p << "^" << params[p]=0A= end=0A= }=0A= end=0A= return cstr=0A= end=0A= =0A= def crumb_add( bite )=0A= cbs =3D @cookies=0A= puts cbs.class=0A= cbs =3D cbs[:rdf_crumbs]=0A= if cbs =3D=3D nil=0A= puts "crumb_add::cbs is nil!"=0A= cbs =3D[bite]=0A= else=0A= puts "crumb_add::cbs is OK!"=0A= cbs =3D cbs.split("|")=0A= #don't add a redundant=0A= if cbs.last =3D=3D bite=0A= return=0A= end=0A= # add to=0A= cbs << bite=0A= # cap the cookie queue at 5 using fifo=0A= if cbs.length > @@MAX_CRUMBS=0A= cbs.delete_at(0)=0A= end=0A= end=0A= crumbs_set( cbs )=0A= end=0A= =0A= def crumbs_set( mouthful )=0A= cr =3D ""=0A= mouthful.each { |m|=0A= if cr =3D=3D ""=0A= cr =3D m=0A= else=0A= cr << "|" << m=0A= end=0A= }=0A= puts cookies.class=0A= cookies[:rdf_crumbs] =3D cr=0A= end=0A= =0A= def get_crumbs()=0A= crs =3D @cookies["rdf_crumbs"]=0A= if crs =3D=3D nil=0A= return []=0A= else=0A= return crs.split("|")=0A= end=0A= end=0A= =0A= end=0A= =0A= =0A= class Bread=0A= include Crumbs=0A= # if initialize is used you must use super=0A= # otherwise just leave out initialize=0A= def initialize=0A= super=0A= # initialization specific to Bread here=0A= end=0A= end=0A= ------=_NextPart_000_001E_01C6169C.8B417570--