This is my code and I'm not sure why this doesn't return the expected result: A Bunny Hops
text= "a bunny hops"
final = text.split.each{|i| i.capitalize}.join(' ')
puts final
This is my code and I'm not sure why this doesn't return the expected result: A Bunny Hops
text= "a bunny hops"
final = text.split.each{|i| i.capitalize}.join(' ')
puts final
Do as below using Array#map
:
text.split.map { |i| i.capitalize }.join(' ')
Corrected and short code :
text= "a bunny hops"
final = text.split.map(&:capitalize).join(' ')
puts final
# >> A Bunny Hops
Why didn't your one worked :
Because Array#each
method returns the receiver itself on which it has been called :
text= "a bunny hops"
text.split.each(&:capitalize) # => ["a", "bunny", "hops"]
But Array#map
returns a new array
text.split.map(&:capitalize) # => ["A", "Bunny", "Hops"]
I would do it as below using String#gsub
:
text= "a bunny hops"
text.gsub(/[A-Za-z']+/,&:capitalize) # => "A Bunny Hops"
Note: The pattern I used here with #gsub
, is not the trivial one. I did it as per the string have been given in the post itself. You need to change it as per the text string samples you will be having. But the above is a way to do such things with short code and more Rubyish way.
instead of splitting the string into an array, and then joining it back together, just use regular expressions:
text = "a bunny hops"
final = text.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
puts final
full disclosure, this how the Ruby on Rails titleize
method works. View Source.
Taking your existing code, just add ! next to capitalize:
final = text.split.each{|i| i.capitalize!}.join(' ')
or replace 'each' with 'map':
final = text.split.map{|i| i.capitalize}.join(' ')
and your goal is accomplished :)