2009年6月7日 星期日

[Ruby] 在Ruby程式使用單複數轉換

取用Rails中內含的Module - ActiveSupport

Module:
ActiveSupport::Inflector
ActiveSupport::Inflector::Inflections


如果你有裝 rails 那裡面就包含了ActiveSuppt,如果沒有的話可以透過以下指令來安裝:

C:\> gem install rails

或是只裝Active Support
C:\> gem install activesupport


Sample Code:
require 'active_support/inflector'
require 'active_support/inflections'

puts "box".pluralize # => "boxes"
puts "child".pluralize # => "children"

puts "pets".singularize # => "pets"
puts "women".singularize # => "woman"


It's so cool!!
Related Posts Plugin for WordPress, Blogger...