Search code examples
rubyenumerable

Inplace enumeration


I am using ruby 1.8.7 and ruby on rails 3.x . I have many lines like this

lines = lines.map {|e| e.strip}
lines = lines.map {|e| e.upcase}
lines = lines.map {|e| sanitize_element(e)}

Rather than assigning new values to lines every time is there a better way to handle this. I know I can do

lines = lines.map {|e| sanitize_element(e.strip.upcase) }

but that is not the main point of this question. The main thing is to find if there is a way to handle the above case without assigning value to lines every time.

Basically I am looking for a solution as elegant as this, but I know there isn't a map! in Enumerable.

lines.map! {|e| e.strip}

Just making sure that I am not missing out on a ruby feature.


Solution

  • Yes, by using Array#map!:

    lines.map! { |e| e.strip }
    lines.map! { |e| e.upcase}
    # ...
    

    Often, an immutable method like map is paired with a dangerous one like map!, which causes the receiver to be modified. I recommend against using these, since half the point of nice, functional-style enumerable programming is to get the benefits of immutability (referential transparency, etc.). But if you're going to reassign on each enumeration, you might as well use map!.