September 2006
5 posts
2 tags
Trim whitespace from the end of a string in Ruby
Use rstrip. "string with whitespace ".rstrip
Sep 29th
3 tags
Rails: converting an array to a readable sentence
There is a nifty method in ActiveSupport called to_sentence: >> fruits = ['apple','banana','peach'] => ["apple", "banana", "peach"] >> fruits.to_sentence => "apple, banana, and peach" >> fruits.to_sentence :skip_last_comma => true => "apple, banana and peach" I prefer to drop the last comma - use :skip_last_comma => true to do this.
Sep 26th
2 tags
Ruby: increment operator?
There is no increment operator in Ruby (++ will not work). The closest solution is: i += 1
Sep 25th
3 tags
Ruby: declaring private methods
Private methods are useful in Rails where you need to define a method inside a controller that does not map to an action. private def my_method # code here end Unlike in other languages, in Ruby the ‘private’ keyword acts as a toggle - all methods after the ‘private’ keyword will be made private unless you call a different privacy method before them - ‘public’...
Sep 21st
2 tags
Rails: copying development database structure to...
Use the following command in your app’s top level directory to copy the structure of the development database to the test db: rake clone_structure_to_test
Sep 1st