From: "Hal E. Fulton" Date: 2001-11-17T04:12:08+09:00 Subject: [ruby-talk:25604] Re: Arrays, iterators, and map/collect ----- Original Message ----- From: Robert Feldt To: ruby-talk ML Sent: Friday, November 16, 2001 4:19 AM Subject: [ruby-talk:25403] Re: Arrays, iterators, and map/collect > On Fri, 16 Nov 2001, Hal E. Fulton wrote: > > > But what if I want to do an operation > > conditionally? Is this still a good way to do it, > > in a case where the mapped value is > > sometimes/always the same as the original? > > > > strings.map! { |s| if s[0]==?a then s else "pre." + s end } > > > I would do it like this but allow myself to shorten it to > > strings.map! {|s| s[0]==?a ? s : ("pre." + s)} > > since its a one-liner. > > Don't you feel it is natural? Why? > I'm having difficulty expressing myself. This example is natural enough. One problem I have is: When I travel through an array, I expect to use an iterator. What if the map! operation is really only a side effect? I guess it is possible to do other stuff inside the block, but that makes it less obvious what is happening. I guess I would like to iterate through an array performing arbitrary operations and have the option to change the values of array elements if I wished. "replace" is OK I suppose... I haven't tried it. I don't like the idiom of explicitly indexing into the array for this case... Thanks, Hal