PHP to Ruby
Convert PHP code into Ruby!
-
rtrim
Posted on June 19th, 2009 No commentsThe rtrim function strips whitespace (or other characters) from the end of a string.
PHP
echo rtrim('Work Hard. Play Harder.', ' Play Harder.'); => // 'Work Hard.'
Ruby
substitute = 'Play Harder.'; puts "Work Hard. Play Harder.".gsub(/[#{substitute}]+$/, '') => # 'Work Hard.'
-
ltrim
Posted on June 16th, 2009 No commentsThe ltrim function strips whitespace (or other characters) from the beginning of a string.
PHP
echo ltrim('John and I love icecream', 'John and '); => // 'I love icecream'
Ruby
substitute = 'John and '; puts "John and I love icecream".gsub(/^[#{substitute}]+/, '') => # 'I love icecream'
-
trim
Posted on June 13th, 2009 No commentsThe trim function is mainly used to remove whitespace from the beginning and end of strings, although it can be used to remove other characters too.
PHP
echo trim(" Hello World "); => // 'Hello World'
Ruby
puts " Hello World ".strip => # 'Hello World'
The trim() function can also take a second argument which allows the removal of an arbitrary character.
PHP
echo trim("\n\nHello World\n\n", "\n"); => // 'Hello World'
Ruby
substitute = '\n'; puts "\n\nHello World\n\n".gsub(/^[#{substitute}]+|[#{substitute}]+$/, '') => # 'Hello World'


