Hello -- On Wed, 28 Nov 2001, Hugh Sasse Staff Elec Eng wrote: > On Sat, 24 Nov 2001, MenTaLguY wrote: > > > > How might I add some instance variables to an existing class, such > > > as String, and have them initialized properly? > > > > Two possibilities come to mind immediately: > > > > class String > > attr_writer :pos > > def pos > > @pos = 0 if not instance_variables.include? "@pos" > > @pos > > end > > end > > This didn't work, it still gives me nil, as does David Alan Black's > solution of @pos ||= 0. [...] > This didn't work either. The codes all blow up where I use a defualt > parameter > > def any(chars, start=@pos, finish=self.length) > > even if I change this to > def any(chars, start=pos, finish=self.length) > > start always ends up as nil instead of 0. Hmmm.... I must be writing/testing the wrong thing. Here's what I wrote: class String attr_writer :pos def pos @pos ||= 0 end def thing(start = pos) start end end p "hi".thing # => 0 p "bye".pos # => 0 Where do you get nil? David -- David Alan Black home: dblack / candle.superlink.net work: blackdav / shu.edu Web: http://pirate.shu.edu/~blackdav