Capitalize and capitalize! (i don't quite understand)



I have been going back through the Ruby tuts to see where I’m
going wrong.

The bit I’m getting confused about is:

print "This is my question?"
answer = gets.chomp
answer2 = answer.capitalize

I don't understand why we seem to format the variable
'answer' twice. (fiest as 'answer' and then as 'answer2'

Using 'capitalize!' gets me through the exercise and the
explanation seems more complicated than need be, so I’m sure I’m missing something.

Could somone please reword the explanation so i can
understand why i use the 'answer2=answer.capitalize' instead of the simpler:

print "This is my question?"
answer = gets.chomp

Any help is appreciated. :smile:


Hey, capitalize is a string method that makes the first character uppercase and everything else lowercase.

When you use capitalize on a string, it does not actually change the original -- it just returns a copy of that string, capitalized.

my_string = "heLLo"
puts my_string.capitalize # => "Hello"
puts my_string # => "heLLo"

If you really want to actually modify the original string, then you use an exclamation mark: !

my_string = "heLLo"
puts my_string.capitalize! # => "Hello"
puts my_string # => "Hello"

The exercise wants you to understand this difference and use the ! to actually modify the original string.


I'm sorry if this sounds a little daft, but then why must we use an "answer2"?
can we not just do answer.capitalize!


you don't need answer2 at all. I think they just meant to show you that you could alternately create a new variable (answer2) to store the capitalized answer, but if you did that, then you would need to use answer2 when printing out the variable. You are thinking exactly right -- they just made it confusing.


Hi, here's the code I used:

print "What's your first name?"
first_name = gets.chomp
first_name2 = first_name.capitalize

print "What's your last name?"
last_name = gets.chomp
last_name2 = last_name.capitalize

print "What city do you live in?"
city = gets.chomp
city2 = city.capitalize

print "What state/province do you live in?"
state = gets.chomp
state2 = state.upcase

print "Your name is #{first_name} #{last_name} and you live in #{city}, #{state}!"

Hope that helps!



I am working through the same question now.
And I am sure i have the code right, basically as above by @surfista but it is coming back with
'Oops, try again. Make sure to call capitalize! on first_name, last_name, and city.'
Really not sure what's wrong, like i said I think it's right. If someone has any thoughts they'd be gratefully received.


This lesson is confusing... which is strange, because all the other lessons I've done so far were easy. I mean, why can't you just capitalize instead of telling the computer to do it for you? Just hold the shift key :\


I was thinking the same thing at first. I had to remember that this code is to make a form for the user. The user might not use the shift key, hence needing to automatically capitalize the information after the end user inputs the characters.


This worked for me too...


Put it this way:
You have a user who just registered on your site.
Now you want to send an email to this person saying "Thank you #{full_name}, for registering!"
What if the user didn't capitalize his/her name? You should always capitalize names.


You are probably missing a ! on one or more of you .capitalize or .upcase
here is the code i passed with

print "What's your first name?"
first_name = gets.chomp.capitalize!
print "What's your last name?"
last_name = gets.chomp.capitalize!
print "What city do you live in?"
city = gets.chomp.capitalize!
print "What state do you live in? (abbreviation)"
state = gets.chomp.upcase!

puts "Your name is #{first_name} #{last_name}!"
puts "You live in #{city} #{state}!"


thank u.. this rescued me


I'm understanding more thanks to everyone's answers here. Just want to make sure, could you simply type:

print "What's your first name?"
first_name = gets.chomp.capitalize

(without the ! wouldn't that capitalize the user's input without answer and answer2 being needed? am i overthinking this?)

compared to the HTML course, Ruby seems to be poorly explained or it just is harder to communicate solely through text