We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

jccovey on 06/20/08


Tagged

String camelize pluralize


Versions (?)


String


Published in: Ruby 


  1. class String
  2.  
  3. def camelize(word, first_letter_in_uppercase = true)
  4. if first_letter_in_uppercase
  5. word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  6. else
  7. word.first + camelize(word)[1..-1]
  8. end
  9. end
  10.  
  11. def pluralize(num)
  12. if num == 1
  13. self
  14. else
  15. self + "s"
  16. end
  17. end
  18.  
  19. end

Report this snippet 

You need to login to post a comment.