Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Thank you just what I was looking for, useful for converting headings to 'sentence case' Commented Sep 23, 2017 at 11:12
  • 3
    word[0] = word[0].upcase Commented May 12, 2019 at 16:53
  • @David. NO! That changes the values of the words in the array that #collect is called on. That is a bad side-effect. Commented May 14, 2019 at 17:36
  • I was showing a simpler way for capitalizing the first letter of a word, replacing the inner 3 lines of this solution, which I made clear by using the word variable. Of course, if you have more words, just call them on all of them! ;) words.map{|word| word[0] = word[0].upcase} Commented May 14, 2019 at 21:52
  • @David. Your code amounts to #capitalize! and not #capitalize. The latter returns a new String while the former modifies the receiver of the method (in this case the receiver is word and the method is #[]). If you used your code inside of a #collect block then you'd end up with two different arrays with the same String objects in each of them (and the Strings would have been modified). That is not something you'd normally want to do. Even if you're aware of this, other readers should understand this. Commented May 15, 2019 at 23:49