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.
Chris (Guest)
on 2006-03-14 18: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
Chris H. (Guest)
on 2006-03-14 18: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
unknown (Guest)
on 2006-03-14 18: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 (removed_email_address@domain.invalid)
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
Chris (Guest)
on 2006-03-15 13: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
Alex Y. (Guest)
on 2006-03-15 13: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
Alex Y. (Guest)
on 2006-03-15 13:39
(Received via mailing list)
Alex Y. wrote:
> "Help oUr kinGS".scan(/(\A|\W)(\w)/).collect{|s| s[1]}.join.upcase
Better:
"help our kings".gsub(/(\w)\w+\W*/, '\1').upcase
Chris (Guest)
on 2006-03-15 13: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 :)
Alex Y. (Guest)
on 2006-03-15 14: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
Thibaut Barrère (Guest)
on 2006-03-15 14: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)
unknown (Guest)
on 2006-03-15 15: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 (removed_email_address@domain.invalid)
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.