Ruby: Formatting with String Methods (6/7)


#1

I have a hard time understanding what is happening when you add the exclamation point (!)

Here is 2 scripts that give me the exact same output:

Script 1
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 are you from? "
city = gets.chomp.capitalize

print "What state or province are you from? "
state = gets.chomp.upcase

puts “Your name is #{first_name} #{last_name} and you’re from #{city}, #{state}!”

Script 2
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 = gets.chomp
city.capitalize!

print "What state or province are you from? "
state = gets.chomp
state.upcase!

puts “Your name is #{first_name} #{last_name} and you’re from #{city}, #{state}!”

The correct answer in this exercise is script 2. Can someone explain to me in baby steps what (!) do in the second script?


#2

Nothing happens, it’s not an operator, it’s part of the method name (by changing the name you are now calling a different method)


#3

Thank’s ionatan.
But I’m still puzzled.
Here is an other script:

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 = gets.chomp
city.capitalize

print "What state or province are you from? "
state = gets.chomp
state.upcase

puts “Your name is #{first_name} #{last_name} and you’re from #{city}, #{state}!”

In this case, nothing get capitalized. I’m still unable to understand what the exclamation point do.


#4

Doesn’t do anything. It’s like the letter T. You use it as part of names. If you use a different method, then you ought to expect a different result.

And, when using just about any method, you gotta somehow look up what it promises to do.


#5

I just want to say thank you for taking the time to answer me. This might be really simple but it just doesn’t click in my brain. I think I have more of a hard time with what they say in this exercise. They give you this code:

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

Then later they explain:
The next line might look a little strange, we don’t assign the result of capitalize to a variable. Instead you might notice the ! at the end of capitalize. This modifies the value contained within the variable answer itself. The next time you use the variable answer you will get the results of answer.capitalize

This right there: The next time you use the variable answer you will get the results of answer.capitalize Does that mean that if I would not put (!), the next time I would use the variable answer without using (!) the answer would not be capitalized?


#6

Since capitalize and capitalize! or two different methods, you might read what each one does and pick the one that serves your situation better

https://ruby-doc.org/core-2.5.0/String.html#method-i-capitalize
https://ruby-doc.org/core-2.5.0/String.html#method-i-capitalize-21


#7

I don’t know where you send my but this library is full of information, I will defiantly understand now. Thank you ionatan, you are the best!


#8

That page describes string methods. Generally you’d visit a search engine and type something like
ruby doc string capitalize

And, the thing to keep in mind here is that you should really only ever write things that you know how they behave, so that you can combine them in ways that make sense, in a way that produces the result you’re after. Guessing at what to write leads nowhere, the better alternative is to go and find out.

When you write something, you have to start out by deciding what should happen, and then you can start researching how to express those things (such as how exactly the methods behave so that you know how you can use them together), and only after this can you start typing. Obviously, as you learn more you start taking shortcuts through this by already knowing how things behave and/or being really good at looking things like this up.


#9

Absolutely! This is why I was enquiring. I don’t understand something unless I can explain it. With the script #2, I will always have answer capitalize later in the script, unless I change something. I guess that made me click! Thank again!


#10

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