-
ucwords
Posted on July 13th, 2009 1 commentThe 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.
-
ucfirst
Posted on July 7th, 2009 No commentsThe ucfirst function returns a string, with the first character capitalized - only if the first character is alphabetic.
PHP
echo ucfirst("ruby is great!"); => // Ruby is great!
Ruby
puts "ruby is great!".capitalize => # Ruby is great!
-
strtoupper
Posted on March 31st, 2009 No commentsReturns the argument provided with all alphabetic characters converted to uppercase.
PHP
echo strtoupper('Ruby is pure OO'); // => RUBY IS PURE OO
Ruby
puts 'Ruby is pure OO'.upcase; # => RUBY IS PURE OO
-
array_change_key_case
Posted on March 28th, 2009 No commentsThis function changes all keys in an array by returning an array with all keys from argument lowercased or uppercased. Numbered indices are left as is.
PHP
$input_array = array('FirSt' => 1, 'SecOnd' => 4); print_r( array_change_key_case($input_array, CASE_UPPER) ); // => array('FIRST' => 1, 'SECOND' => 4);
To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.
Ruby
hash = {'FirSt' => 1, 'SecOnd' => 4} result = hash.inject({}) do |hash, keys| hash[keys[0].upcase] = keys[1] hash end p result # => { 'FIRST' => 1, 'SECOND' => 4 }
PHP, Ruby arrays, hash, keys, lower case, upper case


