Sunday, November 04, 2007

Rails: String#pluralize

Documentation
Returns the plural form of the word in the string.

Examples
  • "post".pluralize #=> "posts"
  • "octopus".pluralize #=> "octopi"
  • "sheep".pluralize #=> "sheep"
  • "words".pluralize #=> "words"
  • "the blue mailman".pluralize #=> "the blue mailmen"
  • "CamelOctopus".pluralize #=> "CamelOctopi"
Usage
I generally use pluralize for creating nice messages for users. The pluralize method can also be helpful for metaprogramming when converting from a class name to a table name.

Test

require 'rubygems'
require 'active_support'
require 'test/unit'
require 'dust'

unit_tests do
test "change singular word to plural word" do
assert_equal "names", "name".pluralize
end
end

Saturday, November 03, 2007

Rails: String#camelize

Documentation
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to ":lower" then camelize produces lowerCamelCase.

camelize will also convert ’/’ to ’::’ which is useful for converting paths to namespaces

Examples
  • "active_record".camelize #=> "ActiveRecord"
  • "active_record".camelize(:lower) #=> "activeRecord"
  • "active_record/errors".camelize #=> "ActiveRecord::Errors"
  • "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
Usage
I generally use camelize when metaprogrammming to convert from a underscored version of a class name.

Test

require 'rubygems'
require 'active_support'
require 'test/unit'
require 'dust'

unit_tests do
test "change underscored word to camelized word" do
assert_equal "AClassName", "a_class_name".camelize
end
end

Friday, November 02, 2007

Rails: Hash#assert_valid_keys

Documentation
None

Usage
Hash#assert_valid_keys gives you the ability to verify that each of the keys in the Hash are expected. For example, if you take an options Hash as an argument to a method you may want to validate that only expected options are passed as part of the hash; assert_valid_keys gives you the ability to list the keys you expect.

Test

require 'rubygems'
require 'active_support'
require 'test/unit'
require 'dust'

unit_tests do
test "raise argument error on unexpected key" do
assert_raises ArgumentError do
{:invalid_key => :a, :valid_key => :b}.assert_valid_keys(:valid_key)
end
end
end

Thursday, November 01, 2007

Rails: Array#to_sentence

Documentation
Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options:
  • :connector: The word used to join the last element in arrays with two or more elements (default: "and")
  • :skip_last_comma: Set to true to return "a, b and c" instead of "a, b, and c".
Usage
Array#to_sentence is perfect for joining information stored in an array that needs to be presented in a human readable format (e.g. an array of errors).

Test

require 'rubygems'
require 'active_support'
require 'test/unit'
require 'dust'

unit_tests do
test "words are joined nicely" do
expected = "one, two, and three"
assert_equal expected, ['one', 'two', 'three'].to_sentence
end
end

2 weeks of ActiveSupport

Recently I was browsing ActiveSupport and noticed a few methods that I wasn't, but should have been using. This led me to the idea of doing some quick entries on the various methods of ActiveSupport that may not get as much use as they should. So, following this entry, I'll be publishing something short every day for 2 weeks with information on an ActiveSupport method.

*30 days became 2 weeks after several complaints. If there are methods of ActiveSupport that I didn't get to, I encourage you to do a few quick blog entries and leave links in comments. If enough come through I'll do a follow up with links to your blog entries.