Alle mercoledì 15 agosto 2007, Haze Noc ha scritto:
> Does anyone have any good ways of transferring a string to random chars?
> For example, our string is hello, and i want HeLlO or something.. Is
> there an easy way of doing this without splitting the string and reading
> each value of an array?
>
> thanks

You can try this:

  def rand_case str
    res = ''
    str.size.times do |i|
      res << str[i].chr.send(rand >= 0.5 ? :upcase : :downcase)
    end
    res
  end

This creates a new string. If you want to modify the original string, you can 
use this

  def rand_case! str
    str.size.times do |i|
      str[i] str[i].chr.send(rand >= 0.5 ? :upcase : :downcase)
    end
    str
  end

I hope this helps

Stefano