Forum: Ruby on Rails Can you better this String acronym method?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-14 17:23
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-14 17:34
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-14 17:43
(Received via mailing list)
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 " "
> end

How 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
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-15 12:17
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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-15 12:34
(Received via mailing list)
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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-15 12:39
(Received via mailing list)
Alex Young wrote:
> "Help oUr kinGS".scan(/(\A|\W)(\w)/).collect{|s| s[1]}.join.upcase
Better:
"help our kings".gsub(/(\w)\w+\W*/, '\1').upcase
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-15 12:50
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 :)
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-15 13:41
(Received via mailing list)
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
91eb330fb36d1e03c856574dfb77d2bc?d=identicon&s=25 Thibaut Barrère (Guest)
on 2006-03-15 13:56
(Received via mailing list)
> 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)
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-15 14:06
(Received via mailing list)
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
This topic is locked and can not be replied to.