The ! at the end of the .capitalize


#1

https://www.codecademy.com/courses/learn-ruby/lessons/putting-the-form-in-formatter/exercises/formatting-with-string-methods?action=lesson_resume&link_content_target=interstitial_lesson

I get the same result no matter if i use the ! at the end of the .capitalize or not. even though i get the same results, it is still marked wrong.

```

print "What’s your first name? "
first_name = gets.chomp
first_name = first_name.capitalize
print "What’s your last name? "
last_name = gets.chomp.capitalize!
print "What city are you from? "
city = gets.chomp.capitalize!
print "What state or province are you from? "
state = gets.chomp.upcase!
print "Your name is #{first_name} #{last_name} and your are from #{city}, #{state}!“
puts”"
print first_name
print last_name

<do not remove the three backticks above>

Hi, little help with an exercise
#2

If the lesson expects us to use the bang (!) then we should. Humor the author. Write your first_name the way you have the last_name.

Those lines should be removed.


#3

I did some looking into it. and the “!” at the end is the same as starting the line with
variable=
but if you put it as part of the initializer, then it is not needed.

variable=gets.chomp.capitalize
-------is the same as--------
variable=gets.chomp
variable.capitalize!


#4

Not exactly. The bang is an in-place operation that does not require assignment. It’s difficult to tell what the objective of this lesson is. Since there is an assignment being made, it does not make sense to use bang (if that is what your point is, that means I agree with you).

first_name = gets.chomp.capitalize

Let’s run an experiment on the prescribed code:

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? (xx) "
state = gets.chomp.upcase!

puts "Your name is #{first_name.nil? ? 'nil' : first_name} #{last_name.nil? ? 'nil' : last_name} and you live in #{city.nil? ? 'nil' : city}, #{state.nil? ? 'nil' : state}."

Notice I’ve written conditional expressions in the string. Now when we run the code and enter no text, we get this…

What's your first name? 
What's your last name? 
What city do you live in? 
What state? (xx) 
Your name is nil nil and you live in nil, nil.

Now if we remove the bangs,

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? (xx) "
state = gets.chomp.upcase

puts "Your name is #{first_name.nil? ? 'nil' : first_name} #{last_name.nil? ? 'nil' : last_name} and you live in #{city.nil? ? 'nil' : city}, #{state.nil? ? 'nil' : state}."

we get this upon entering no data:

What's your first name? 
What's your last name? 
What city do you live in? 
What state? (xx) 
Your name is   and you live in , .

The bang method returns nil if there is no change in the data object (the inputed string). Without the bang, the method returns what it is given whether there is a change or not.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.