I am trying to capitalize first letter of each word in a string of
words, which is in a given string. I see there is a reference to this
at:
http://www.java2s.com/Code/Ruby/String/Addtwomethodtostringclasstocapitalizefirstletter.htm
which states to add two method to string class to capitalize first
letter
class String
def capitalize_first_letter
self[0].chr.capitalize + self[1, size]
end
def capitalize_first_letter!
unless self[0] == (c = self[0,1].upcase[0])
self[0] = c
self
end
end
end
My question is, if I create this String class -- what is it? Is it a
model? A helper? Where in the world would you put this creature in a
Rails framework? Thanks, Janna
Well, those two methods won''t give you what you want, if you really
want
to capitalize the first letter of every word in a sentence. Also, if you
just want to capitalize the first letter of the string, the built-in
capitalize method will do that for you:
''some string''.capitalize => ''Some string''
If you want to capitalize each word in a string, you could do this (a naive
approach):
''some string''.split('' '').map {|w|
w.capitalize }.join('' '')
If you then wanted to make this built-in to the String class, as you were
referencing in your original request, you could add a method that does this
to the String class by reopening it (like the example you posted):
class String
def cap_words
self.split('' '').map {|w| w.capitalize }.join(''
'')
end
end
You could put this snippet of code in a file in config/initializers or (as
my preferred approach would be) in a file in lib (like
lib/extensions/string.rb) that is then required via an initializer (like
config/initializers/requires.rb): require ''extensions/string''
Now you have reopened String to add this method. Whether that''s a good
idea
is another matter entirely. :)
--
Benjamin Curtis
http://railskits.com/ - Ready-made Rails code
http://catchthebest.com/ - Team-powered recruiting
http://www.bencurtis.com/ - Personal blog
On Tue, May 26, 2009 at 11:48 AM, JannaB
<mistressjanna-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:
>
> I am trying to capitalize first letter of each word in a string of
> words, which is in a given string. I see there is a reference to this
> at:
>
>
>
http://www.java2s.com/Code/Ruby/String/Addtwomethodtostringclasstocapitalizefirstletter.htm
>
> which states to add two method to string class to capitalize first
> letter
>
>
> class String
> def capitalize_first_letter
> self[0].chr.capitalize + self[1, size]
> end
>
> def capitalize_first_letter!
> unless self[0] == (c = self[0,1].upcase[0])
> self[0] = c
> self
> end
> end
> end
>
> My question is, if I create this String class -- what is it? Is it a
> model? A helper? Where in the world would you put this creature in a
> Rails framework? Thanks, Janna
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On May 26, 2009, at 11:48 AM, JannaB wrote:> > I am trying to capitalize first letter of each word in a string of > words, which is in a given string. I see there is a reference to this > at: >String#titleize is your friend... >> "this is some string. here is another! a question? oh my.".titleize => "This Is Some String. Here Is Another! A Question? Oh My."> http://www.java2s.com/Code/Ruby/String/Addtwomethodtostringclasstocapitalizefirstletter.htm > > which states to add two method to string class to capitalize first > letter > > > class String > def capitalize_first_letter > self[0].chr.capitalize + self[1, size] > end > > def capitalize_first_letter! > unless self[0] == (c = self[0,1].upcase[0]) > self[0] = c > self > end > end > end > > My question is, if I create this String class -- what is it? Is it a > model? A helper? Where in the world would you put this creature in a > Rails framework? Thanks, Janna > >
Indeed... I got wrapped around the axle of re-opening classes in Ruby and forgot to mention the Rails extensions. :) -- Benjamin Curtis http://railskits.com/ - Ready-made Rails code http://catchthebest.com/ - Team-powered recruiting http://www.bencurtis.com/ - Personal blog On Tue, May 26, 2009 at 12:34 PM, Philip Hallstrom <philip-LSG90OXdqQE@public.gmane.org> wrote:> > > > On May 26, 2009, at 11:48 AM, JannaB wrote: > > > > > I am trying to capitalize first letter of each word in a string of > > words, which is in a given string. I see there is a reference to this > > at: > > > > String#titleize is your friend... > > >> "this is some string. here is another! a question? oh my.".titleize > => "This Is Some String. Here Is Another! A Question? Oh My." > > > > > > > > http://www.java2s.com/Code/Ruby/String/Addtwomethodtostringclasstocapitalizefirstletter.htm > > > > which states to add two method to string class to capitalize first > > letter > > > > > > class String > > def capitalize_first_letter > > self[0].chr.capitalize + self[1, size] > > end > > > > def capitalize_first_letter! > > unless self[0] == (c = self[0,1].upcase[0]) > > self[0] = c > > self > > end > > end > > end > > > > My question is, if I create this String class -- what is it? Is it a > > model? A helper? Where in the world would you put this creature in a > > Rails framework? Thanks, Janna > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
toamitkumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-May-27 18:11 UTC
Re: Capitalize first letter of each word
titleize --->>
def titleize(word)
humanize(underscore(word)).gsub(/\b(''?[a-z])/) { $1.capitalize }
end
output:
"at&t" --> "At&T" (not the desired output)
"man-eater --> "Man Eater" (not the desired output)
"at&t".split('' '').map {|w| w.capitalize
}.join('' '') ---> "At&t" seems
to be better approach.
--
Amit
On May 26, 4:56 pm, Benjamin Curtis
<benjamin.cur...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Indeed... I got wrapped around the axle of re-opening classes in Ruby and
> forgot to mention the Rails extensions. :)
> --
> Benjamin Curtishttp://railskits.com/- Ready-made Rails
codehttp://catchthebest.com/- Team-powered recruitinghttp://www.bencurtis.com/-
Personal blog
>
> On Tue, May 26, 2009 at 12:34 PM, Philip Hallstrom
<phi...-LSG90OXdqQE@public.gmane.org> wrote:
>
> > On May 26, 2009, at 11:48 AM, JannaB wrote:
>
> > > I am trying to capitalize first letter of each word in a string
of
> > > words, which is in a given string. I see there is a reference to
this
> > > at:
>
> > String#titleize is your friend...
>
> > >> "this is some string. here is another! a question? oh
my.".titleize
> > => "This Is Some String. Here Is Another! A Question? Oh
My."
>
>
>http://www.java2s.com/Code/Ruby/String/Addtwomethodtostringclasstocap...
>
> > > which states to add two method to string class to capitalize
first
> > > letter
>
> > > class String
> > > def capitalize_first_letter
> > > self[0].chr.capitalize + self[1, size]
> > > end
>
> > > def capitalize_first_letter!
> > > unless self[0] == (c = self[0,1].upcase[0])
> > > self[0] = c
> > > self
> > > end
> > > end
> > > end
>
> > > My question is, if I create this String class -- what is it? Is
it a
> > > model? A helper? Where in the world would you put this creature
in a
> > > Rails framework? Thanks, Janna
>
>