From: Josh Cheek Date: 2011-09-04T08:22:49+09:00 Subject: Re: arrays(setters and getters) --90e6ba53b0d478e25704ac11c14b Content-Type: text/plain; charset=ISO-8859-1 On Sat, Sep 3, 2011 at 6:16 PM, jack jones wrote: > # whats wrong with that > class Whatever > attr_accessor :array > @array > def initialize > @array = [] > @array = ["apple", "banana", "peach"] > end > def array []= (i,value) # setter > @array[i] = value > end > def array[] (i) #getter > @array[i] > end > end > > shehio = Whatever.new > puts shehio.inspect > puts shehio.array > puts shehio.array[0] > puts shehio.array[1] > puts shehio.array[2] > shehio.array[0] = 1 > shehio.array[0] = 2 > shehio.array[0] = 3 > puts shehio.array[0] > puts shehio.array[1] > puts shehio.array[2] > > -- > Posted via http://www.ruby-forum. whatever.array returns the array object, then whatever.array.[]=(1,2) is invoked on the array object, you can't define that from your whatever class unless you define it on the array itself, in the initialize method or something. Not to worry, though, array already implements that interface and it does what you're trying to define. Just do this: class Whatever attr_accessor :array def initialize @array = ["apple", "banana", "peach"] end end --90e6ba53b0d478e25704ac11c14b--