title: Change last character of every word in a sentence to a capital using Ruby
body: <p>I was challenged to write some Ruby code that changes the last character of every word in a sentence into a capital. Such that the string:</p>
  
  <pre><code>"script to convert the last letter of every word to a capital"
  </code></pre>
  
  <p>becomes</p>
  
  <pre><code>"scripT tO converT thE lasT letteR oF everY worD tO A capitaL"
  </code></pre>
  
  <p>This is what I ended up using:</p>
  
  <pre><code>"script to convert the last letter of every word to a capital".split.map{|w|w&lt;&lt;w.slice!(-1).chr.upcase}.join' '
  </code></pre>
  
  <p>For those interested as to what is going on here is an explanation.</p>
  
  <p><strong>split</strong> will split the sentence up into an array, the default delimiter is a space and with Ruby you don't need to use brackets here.</p>
  
  <p><strong>map</strong> the array from <code>split</code> is passed to map which opens a block and process each word (w) in the array. the block <strong>slice!</strong>(s) off the last character of the word and converts it to a <strong>chr</strong> (a character not ASCII code) and then capitalises <strong>upcase</strong> it.  This character is now appended <strong>&lt;&lt;</strong> to the word which is missing the sliced last letter.  Finally the array of words is now <strong>join</strong> together with a <strong>' '</strong> to reform the sentence.</p>