capitalize!' (with the exclamation mark on the end) actually changesx’
itself:
a = “boo”
=> “boo”
a.capitalize!
=> “Boo”
a
=> “Boo”
Notice how `a’ has a new value. Now, what if we try to capitalize!
again?
a.capitalize!
=> nil
a
=> “Boo”
It returns nil. a' is still "Boo" - the thing is, capitalize! returns nil becausea’ didn’t change. With your example, This' is already capitalized, so no change occurred. What you really want isx.capitalize’ - it just
returns the value of x capitalized, whatever that is:
a = “boo”
=> “boo”
a.capitalize
=> “Boo”
a
=> “boo”
It also doesn’t change `x’ - not that it would matter much in your given
example.
Also, though it’s probably just your test, but note that you’re not
saving
the result of `collect’ anywhere. As an example:
a = %w(This is a test.)
=> [“This”, “is”, “a”, “test.”]
a = %w(This is a test)
=> [“This”, “is”, “a”, “test”]