Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • ucwords

    Posted on July 13th, 2009 RubyLove 1 comment

    The ucwords() function turns the first character of each word in a string to upper-case, if the first character is alphabetic.

    PHP

    echo ucwords("ruby is easy.");
    => // Ruby Is Easy.

    Ruby doesn’t have a function which can capitalize all the words in a string - so to accomplish this it’s a little bit harder. You need to split the string into words, then capitalize the first character of each word, and then finally join all the words back into a string.

    Ruby

    puts "ruby is easy.".split(' ').select {|w| w.capitalize! || w }.join(' ');
    => # Ruby Is Easy.