Is my code redundant?


#1

Here is a link to the lesson I just finished.

https://www.codecademy.com/courses/ruby-beginner-en-MxXx5/0/6?curriculum_id=5059f8619189a5000201fbcb

I am not getting any error messages, but I feel like my code is redundant. These two statements seem to do the same thing. Is one just shorthand?

first_name = first_name.capitalize
first_name.capitalize!

print "What is your first name?"
first_name = gets.chomp
first_name = first_name.capitalize
first_name.capitalize!
print "What is your last name?"
last_name = gets.chomp
last_name = last_name.capitalize
last_name.capitalize!
print "What city are you from?"
city = gets.chomp
city = city.capitalize
city.capitalize!
print "What state are you from?"
state = gets.chomp
state = state.upcase
state.upcase!
puts "Is this correct? #{first_name} #{last_name}, #{city}, #{state}"

#2

I believe you have to do the first of the two because you are overwriting your variable. It would work if you did the second one but not overwrite it. I don't believe that you need to do both though...


#3

last_name = last_name.capitalize
last_name.capitalize!

nickgrimes50 is correct, these do the same thing.
but the one you should use is the second line, not the first (it is more efficient, and is what they are asking us to do).
The ! after a method overwrites the variable with the new value, so it is the same as the "last_name =" part.