Reputation: 3222
I have this string
Rock Paper Shotgun
I have looked around to find an easy way to get the first letters of each word. Because I want my end result to be:
RPS
Is there a special ruby on rails function for this? If not, how can I achieve such thing?
I found this
str = "nishant nigam"
=> "nishant nigam"
str.split(" ").map {|name| name[0].chr }.join.upcase
=> "NN"
But I was hoping if there is even a simpler method
Upvotes: 0
Views: 1810
Reputation: 619
"Rock Paper Shotgun".split.map(&:first).join.upcase
edit:
irb: [4] pry(main)> "rock Paper Shotgun".split.map(&:first).join.upcase
=> "RPS"
Upvotes: 7
Reputation: 5320
Do it like this:
"Rock Paper Shotgun".split.map{|i| i[0,1].upcase}.join
Upvotes: 0