6/7 Do you need to do an answer2 sort of variable?


#1

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."


#2

There are often many ways to do something, it's not cheating to do something a different way as long as you don't miss the point of the exercise.


#3

Thanks - I suppose I'm just confused with the example code they supply, what is the purpose of the 'answer2' that they show?


#4

It's just here to show the two different ways to modify a variable, either through ! or =.


#5

can someone write out the correct way it is supposed to look?


#6

No, because there's no "correct way", both do the job.


#7

I made mine look like this


print"whats your first name? "

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}!"

Andrew


#8

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.


#9

@khogeweg

@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 ^^.


#10

I agree, I am really unclear on the purpose of answer2 when your way works (and is logical).


#11

I was initially confused but I think its giving you two alternative methods. Either you do:

print"whats your first name? "
first_name = gets.chomp
first_name.capitalize!

or you set first_name2=first_name.capitalize
and then in the puts string at the end you would need to use #{first_name2}

(although this second method results in an error even though it works...)


#12

I came to this Q & A forum for this very same question, and I agree with you - it's simply teaching us two different methods to get the same result.

In hindsight, that's why it says "add the .capitalize! method" in the instructions.

People using both methods will still get the answer, though it's a bit redundant to do so.

I also think that the instructions could use an extra sentence clarifying that these are two different methods, as it's not immediately clear.


#13

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.


#14

There is also a third way which does the trick but is not accepted as solution.

first_name=gets.chomp.capitalize