title: Ruby conditional map and replace
body: <p>I'm trying to conditionally map an array I have:</p>
  
  <p>Array that looks like [ "a", "b", "c" ]</p>
  
  <p>I want to map it inline (using map!) to change anything that matches /b/ and change it to "bb". This doesn't seem to work though, it doesn't seem to be changing the array.</p>
  
  <p>I want it to look like this [ "a", "bb", "c" ]</p>
  
  <p>define the array:</p>
  
  <pre><code>irb(main):001:0&gt; a = [ "a", "b", "c" ]
  =&gt; ["a", "b", "c"]
  </code></pre>
  
  <p>Try with a select chained to a map!, and it shows the changed value for the one element, and it didn't change the original array.</p>
  
  <pre><code>irb(main):002:0&gt; a.select { |e| e =~ /b/ }.map! { |e| e = "bb" }
  =&gt; ["bb"]
  irb(main):003:0&gt; p a.inspect
  "[\"a\", \"b\", \"c\"]"
  </code></pre>
  
  <p>So I tried doing the same thing, but assigning it to a new array, this only creates an array with any changed elements - not what I want</p>
  
  <pre><code>irb(main):004:0&gt; cc = a.select { |e| e =~ /b/ }.map! { |e| e = "bb" } 
  =&gt; ["bb"]
  irb(main):005:0&gt; p cc.inspect
  "[\"bb\"]"
  =&gt; nil
  </code></pre>
  
  <p>What am I missing here? I'm missing some Ruby fundamental knowledge here I think.</p>