Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently using capitalize!:

user_input.capitalize!

But, if the input is capitalized, it returns a nil.

I know I can use the method without !, but it will make a copy of the object and pass that instead of changing the object.

user_input.capitalize

How can I use !, but have it work for all cases?

I tried to downcase everything, then capitalize but the same problem exists in some cases.

What is the best way to capitalize all entries?

This is the context:

The user inputs "works", "Works", "WORKS", or "WoRkS"
case 1) user_input = "works"
case 2) user_input = "Works"
case 3) user_input = "WORKS"
case 4) user_input = "WoRkS"

user_input.capitalize! (case 1,3,4 = "Works"; case 2 = nil)
share|improve this question

2 Answers 2

up vote 4 down vote accepted

! methods, by convention, modify the receiver and return nil if they make no changes. The safest way to do it is to use two lines:

user_name.capitalize!
user_name

or #tap:

user_name.tap do |s|
  s.capitalize!
end

Although, #tap is overkill here.

share|improve this answer

If all you care about is that the input gets capitalized, use capitalize! and don't worry about the nil.

If you can't have a nil at a certain point in your code (for some reason), don't use capitalize!. Instead do this:

user_input = user_input.capitalize

At the end of the day, either way is fine, idiomatic and will get the input into the state you want it. In and of itself, a nil return value isn't a bad thing - although you may not always want it as a final return value.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.7.1.2693\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nosc"], "copyright__copyright__copyright": ["!. Instead do this:\n\nuser_input = user_input.capitalize\n\nAt the end of the day, either way is fine, idiomatic and will get the input into the state you want it. In and of itself, a nil return value isn't a bad thing - although you may not always want it as a final return value.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}