From: Tobias Reif Date: 2001-09-02T02:20:06+09:00 Subject: [ruby-talk:20674] Re: corresponding "non-bang" method to []= Hi Michael, [...] []= is bang. What's the non-bang version? >> > > There's no one. > Does it make sense to have one? Not for me. It's very nice to have non-bang versions of most methods; it results in nicer code: workarounds using dup/clone are less concise and elegant IMHO. >>I'm trying to modify specific members of an array, depending on their >>position in the array ("second and last get a newline" etc.). [...] >>Now after hours of trying, I only got it to work using a dirty hack. >>I'm looking for a nice way. [...] >>In pseudo-code: >> >>an_array.each_index do |x| >> new_val = an_array.at(x) + "\n" >> if (x == 2) or (x == (an_array.size - 1)) >> an_array.set_at(x,new_val) >> end >>end > index = -1 > an_array.collect do |item| > index += 1 > if (index == 2) or (index == (an_array.size - 1)) > item + "\n" > else > item > end > end Like I said: I got it to work (using a counter); but I'm looking for an elegant way. The simple task I'm writing can be done in any language , but I chose Ruby to enjoy writing nice code. Tobi -- Tobias Reif http://www.pinkjuice.com/myDigitalProfile.xhtml go_to('www.ruby-lang.org').get(ruby).play.create.have_fun http://www.pinkjuice.com/ruby/