5

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
Ken
  • 859
  • 2
  • 14
  • 33
  • Does this answer your question? [Ruby capitalize every word first letter](https://stackoverflow.com/questions/13520162/ruby-capitalize-every-word-first-letter) – Kal Oct 12 '22 at 23:35

3 Answers3

12

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.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
2

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.

Stefan
  • 109,145
  • 14
  • 143
  • 218
lightswitch05
  • 9,058
  • 7
  • 52
  • 75
  • Very nice to know this method exists in Rails. I know that the question specifically asks for Ruby but considering the Ruby user base is often synonymous with Rails -- I vote for this answer :) – Kwestion Apr 07 '16 at 18:02
0

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 :)

daremkd
  • 8,244
  • 6
  • 40
  • 66