SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Saturday, April 18, 2009

Capitalize the first character of the sentence in ruby

This is article is for my reference and i found this Rubyonrailsexamples.The following shows how to capitalize the first character in ruby.

For ex str="how to work in ruby on rails"

str.capitalize
=> "How to work in ruby on rails"
Now try this str.split(/\s+/).each{ |word| word.capitalize! }.join(' ')
=>"HowToWorkInRubyOnRails"
or
str.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
=>"How To Work In Ruby On Rails

This article is based on ruby not on rails, since in rails titleize will be used to capitalize the first character of the sentence

No comments :

Post a Comment