Can you better this String acronym method? def acronym name letters=[] name.each_char {|char| letters<<char if char[0]>=65 and char[0]<=90} acronym = letters.join " " end chris -- Posted via http://www.ruby-forum.com/.
are you trying to strip out all non capitalized alpha characters from a string? if so, just use a regular expression # replace anything that is not a capital letter A-Z with a blank acronym = "Here Is A String".gsub(/[^A-Z]/, '''') # => HIAS On 3/14/06, Chris <evilgeenius@gmail.com> wrote:> > Can you better this String acronym method? > > def acronym name > letters=[] > name.each_char {|char| letters<<char if char[0]>=65 and char[0]<=90} > acronym = letters.join " " > end > > chris > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060314/6e72c664/attachment.html
dblack@wobblini.net
2006-Mar-14 16:42 UTC
[Rails] Can you better this String acronym method?
Hi -- On Tue, 14 Mar 2006, Chris wrote:> Can you better this String acronym method? > > def acronym name > letters=[] > name.each_char {|char| letters<<char if char[0]>=65 and char[0]<=90} > acronym = letters.join " " > endHow about: def acronym(name) name.scan(/[[:upper:]]/).join(" ") end David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black
Good tries but wrong, i''m just trying to get an acronym from a string. so "help me please".acronym # => "HMP" or "Help oUr kinGS".acronym # => "HOK" Thanks -- Posted via http://www.ruby-forum.com/.
Chris wrote:> Good tries but wrong, i''m just trying to get an acronym from a string. > > so > > "help me please".acronym # => "HMP" > or > "Help oUr kinGS".acronym # => "HOK""Help oUr kinGS".scan(/(\A|\W)(\w)/).collect{|s| s[1]}.join.upcase -- Alex
Alex Young wrote:> Chris wrote: > >> Good tries but wrong, i''m just trying to get an acronym from a string. >> >> so >> >> "help me please".acronym # => "HMP" >> or >> "Help oUr kinGS".acronym # => "HOK" > > "Help oUr kinGS".scan(/(\A|\W)(\w)/).collect{|s| s[1]}.join.upcaseBetter: "help our kings".gsub(/(\w)\w+\W*/, ''\1'').upcase -- Alex
I really appreciate that Alex,thanks. What about if i wanted a space inbetween each letter? like so : "Help oUr kinGS".acronym # => "H O K" Thanks :) -- Posted via http://www.ruby-forum.com/.
Alex Young
2006-Mar-15 12:39 UTC
[Rails] Re: Re: Can you better this String acronym method?
Chris wrote:> I really appreciate that Alex,thanks. > > What about if i wanted a space inbetween each letter? like so : > > "Help oUr kinGS".acronym # => "H O K" > > Thanks :) >"help our kings".gsub(/(\w)\w+\W*/, ''\1 '').upcase.strip -- Alex
Thibaut Barrère
2006-Mar-15 12:49 UTC
[Rails] Re: Re: Can you better this String acronym method?
> What about if i wanted a space inbetween each letter? like so :str.scan(/\w+/).collect { |w| w[0].chr }.join(myseparator).upcase seems to work for me (put whatever you need in myseparator) -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060315/9d09f5d5/attachment-0001.html
dblack@wobblini.net
2006-Mar-15 13:01 UTC
[Rails] Re: Can you better this String acronym method
Hi -- On Wed, 15 Mar 2006, Chris wrote:> Good tries but wrong, i''m just trying to get an acronym from a string. > > so > > "help me please".acronym # => "HMP" > or > "Help oUr kinGS".acronym # => "HOK"I was assuming you wanted code that worked the way your code did. I guess not :-) How about this: def acronym(string) string.scan(/\b\w/).join.upcase end (or put it in String class if you wish) David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black