From: "Michael T. Richter" Date: 2008-07-10T20:07:53+09:00 Subject: Re: Overloading the []-Operator --=-1okUUBMjCV5nLoc5w3zR Content-Type: text/plain Content-Transfer-Encoding: 7bit On Thu, 2008-07-10 at 19:55 +0900, Matthias Winkelmann wrote: > Hi, > > I'm trying to overload the [ ] operator. It works fine for read-only > access: > > def [](acc) > @myValues[acc] > end > > However, I'd also need write access. I've tried: > > def [](acc)=(value) > @myValues[acc] = value > end > > And get a syntax error on the first line. I'd appreciate any help. > > Matt irb(main):017:0> class Junk irb(main):018:1> def initialize ; @myValues = {} ; end irb(main):019:1> def [] (acc) ; @myValues[acc] ; end irb(main):020:1> def []= (acc,value) ; @myValues[acc] = value ; end irb(main):021:1> end => nil irb(main):022:0> a = Junk.new => # irb(main):023:0> a[5] => nil irb(main):024:0> a[5]=5 => 5 irb(main):025:0> a[5] => 5 irb(main):026:0> -- Michael T. Richter (GoogleTalk: ttmrichter@gmail.com) I'm not schooled in the science of human factors, but I suspect surprise is not an element of a robust user interface. (Chip Rosenthal) --=-1okUUBMjCV5nLoc5w3zR--