From: David Alan Black Date: 2001-09-02T02:55:06+09:00 Subject: [ruby-talk:20679] Re: RCR: non-bang equivalent to []= Hello again -- On Sun, 2 Sep 2001, Tobias Reif wrote: > Hi David, > > > >>[]= is bang; > >>so > >>a version of []= that doesn't modify the receiver > >>would be cool. > >> > > I'm sorry -- I don't understand. What exactly is supposed to happen? > > > > a = [1,2,3,4] > > a.non_bang_set(0) = 100 > > a # => [1,2,3,4] > > > Would the following make sense? > The exact syntax doesn't matter to me, i just would like to get a > non-bang []= ^^^^^^^^^^^^ That phrase sounds self-contradictory to me :-) Anyway.... > Array#set_at > > a = ["a", "b", "c"] > a.set_at 0,"x" -> ["x", "b", "c"] > a -> ["a", "b", "c"] > > Array#set_at! > > a = ["a", "b", "c"] > a.set_at! 0,"x" -> ["x", "b", "c"] > a -> ["x", "b", "c"] Well, since presumably you'd want to save the return value, it would generally be: b = a.set_at(0,"x") as opposed to, say, (b = a.dup)[0] = "x" or b = a.dup b[0] = "x" Personally I find the 'dup' ones clearer. That may be because they are :-) and/or because the word 'set' just doesn't evoke non-modification to me. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav