title: How do I calculate a String's width in Ruby?
body: <p><code>String.length</code> will only tell me how many characters are in the String.  (In fact, before Ruby 1.9, it will only tell me how many bytes, which is even less useful.)</p>
  
  <p>I'd really like to be able to find out how many 'en' wide a String is.  For example:</p>
  
  <pre><code>'foo'.width
  # =&gt; 3
  
  'moo'.width
  # =&gt; 3.5          # m's, w's, etc. are wide
  
  'foi'.width
  # =&gt; 2.5          # i's, j's, etc. are narrow
  
  'foo bar'.width
  # =&gt; 6.25         # spaces are very narrow
  </code></pre>
  
  <p>Even better would be if I could get the first <code>n</code> en of a String:</p>
  
  <pre><code>'foo'[0, 2.en]
  # =&gt; "fo"
  
  'filial'[0, 3.en]
  # =&gt; "fili"
  
  'foo bar baz'[0, 4.5en]
  # =&gt; "foo b"
  </code></pre>
  
  <p>And better still would be if I could strategize the whole thing.  Some people think a space should be 0.25en, some think it should be 0.33, etc.</p>