So, am I just cheating, or is this ok? I tried the code without using an intermediary variable like answer2 in the instructions, and I got the green-light. But is that cheating? Here’s my code (that works):
print "What's your first name?"
first_name = gets.chomp
first_name.capitalize!
print "What's your last name?"
last_name = gets.chomp
last_name.capitalize!
print "What city are you from?"
city_name = gets.chomp
city_name.capitalize!
print "What state or province are you from?"
state_name = gets.chomp
state_name.upcase!
print “Hey, you’re #{first_name}, right? Uhhhh, #{first_name} #{last_name}? Yeah! We’re both from #{city_name}! No no, not that #{city_name}, the one in #{state_name}. Yeah.”
first_name = gets.chomp
first_name2 =first_name.capitalize
first_name.capitalize!
print"whats your second name? "
second_name = gets.chomp
second_name2 = second_name.capitalize
second_name.capitalize!
print "what city are you from? "
city = gets.chomp
city2 = city.capitalize
city.capitalize!
print "what state are you from? "
state = gets.chomp
state2= state.upcase
state.upcase!
puts "Your First Name Is #{first_name}! your second name is #{second_name}! your from #{city} , #{state}!"
I’m totally confused, also because the comments don’t clear much up for me. jarombra, when I type in that code, I do get the green light but it will not capitalize anything for me. When I type in howaa020’s it does capitalize in the final prompt. So, arjofocolovi, I’m confused by what you say, they can’t both be right if the first does not capitalize which is the point of this exercise.
What is the purpose of each line of coding. Obviously it won’t work if I write just the first_name.capitalize! but it will work if I do both. Please clarify.It seems that I need both lines of code for it to work.
@jarombra 's code does capitalize, it’s just that it doesn’t give any output so you can’t see it. You can just add:
puts first_name, last_name, city_name, state_name
if you want to check it yourself.
The ! operator is used to modify directly the object calling the method. That’s why it works just as well as assigning a content with the = operator. All this is explained in the course by the way ^^.
Old thread, but this is still a clarity problem in the course. It’s the only thing in this module that confused me, and as danvineberg mentions, it’s simply not clear that these are two different methods.