title: Capitalization Permutations
body: <p>I wanted to write a snippet of ruby that would take a string and output all possible permutations of capitalizations. Basically, I have a password that I remember, but I don't remember how it is capitalized.</p>
  
  <p>I have the following so far:</p>
  
  <pre><code>def permute(str)
  
    perms = Array.new
    (2 ** str.size).times { perms &lt;&lt; str }
  
    perms.each_index do |i|
      binary = i.to_s(2)
      str_arr = perms[i].split(//)
  
      bin_arr = binary.split(//)
  
      while ( bin_arr.size &lt; str_arr.size )
        bin_arr.unshift('0')
      end
  
      bin_arr.each_index do |b|
        str_arr[b].upcase! if bin_arr[b] == '1'
      end
  
      puts str_arr.to_s
  
    end
  end
  </code></pre>
  
  <p>This works well enough, but I was wondering if any rubyists out there could help me refine it so that it doesn't have to work needlessly on strings with numbers.</p>
  
  <p>For example, the string "tst1" generates:</p>
  
  <pre><code>tst1
  tst1
  tsT1
  tsT1
  tSt1
  tSt1
  tST1
  tST1
  Tst1
  Tst1
  TsT1
  TsT1
  TSt1
  TSt1
  TST1
  TST1
  </code></pre>
  
  <p>The output I'm looking for is:</p>
  
  <pre><code>tst1
  tsT1
  tSt1
  tST1
  Tst1
  TsT1
  TSt1
  TST1
  </code></pre>
  
  <p>Any ideas?</p>