From: Tim Hunter Date: 2007-11-20T09:29:23+09:00 Subject: Re: Assignment without attr= Greg Willits wrote: > Not even sure what to call this for the thread subject > > I want to create a class which is a simple packed set of digits (as a > string) as the one and only attribute of the class. There will be > several methods to extract chunks from and format this string, but > there's only the one attribute. > > For the life of me I can't figure out how to to populate the dang this > to start with, without have to go through an attribute. If it were a > normal multi-attribute object, I'm fine, but in light of there only > being one attribute, I'm trying to work with it a different way. Maybe > I'm just going about it all wrong to start with? > > The essentials: > > class PackedNumber > def initialize > @packedNumber > end > > def methodX > ... > end > > def methodY > ... > end > end > > So, I'd want to work with it like this: > whatever = PackedNumber.new('12345') > Or > whatever = PackedNumber.new > whatever = '12345' # but I don't want whatever to be String > > I'm trying to avoid the obvious, and maybe this is what is not possible > whatever.packedNumber = '12345' > > I tried creating a method just for = but that didn't fly > > def =(chars) > @packedNumber = chars > end > > Feels pretty lame I can't see this, but there ya go. > > -- gw maybe I'm missing something obvious here, but why not just take the initial value as an argument to initialize? class PackedNumber def initialize(packed_number) @packedNumber = packed_number end # other stuff end pn = PackedNumber.new('12345') -- RMagick: http://rmagick.rubyforge.org/