FAQ: Putting the Form in Formatter - Formatting with String Methods

This community-built FAQ covers the “Formatting with String Methods” exercise from the lesson “Putting the Form in Formatter”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Formatting with String Methods

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi guys ! I 'm doing this exercice in order to completely understand the “Methods” but I don’t get it…
Why do we need to have answer2 ?
What’s the point of the exemple below :

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

Is this answer Ok ? :

print "What’s your first name? "
first_name = gets.chomp
first_name = “Laurent”.capitalize
first_name.capitalize!

print "What’s your last name? "
last_name = gets.chomp
last_name = “Potelet”.capitalize
last_name.capitalize!

print "What city are you from? "
city = gets.chomp
city = “Lyon”.capitalize
city.capitalize!

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

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

1 Like

Did you find the answer to this? I’m curious myself :smiley:

What you are doing wrong is that after asking to the user the name, last name, city and state. yourself already in the next line ‘‘anserwing’’ it. The right way to do is:
print "What’s your first name? "
first_name = gets.chomp
first_name.capitalize!

1 Like

Hi, Thiago.

I think what he ment was what is the meaning of the answer2 variable? At least that’s what i’m curious about :smiley:

Is it because you should protect the original input or something? In that case, why the call of the capitalize method on the answer2 variable as well?

1 Like

This appears to be an unfortunately worded set of alternatives for capitalizing a given string.

If one wants to create a NEW variable, assigning the capitalized string content to it, one creates [new variable] “string2 = string.capitalize” (no exclamation mark);

if one wants to revise the string format of the existing variable, one calls upon [existing] variable “string” and reformats its content with “.capitalize!” Thus, “string = string.capitalize!”

Either / or … not both.

10 Likes

Thanks @rrreddd! This makes more sense and I was wondering the same…

@Codecademy should correct this then… Their instructions are very confusing!

5 Likes

That is not how to implement the bang method. It does not get assigned, but happens in situ…

string.capitalize!

The bang method will return nil if no change is made. That means the assignment will be nil, not "String".

str = string.capitalize

or

string.capitalize!
2 Likes

So I run into a lot of names with capital letters in the middle of the name, not just the first letter. Is there a way to maintain case structure and only capitalize the first letter without making the rest lower case?

I was wondering about how to use string.capitalize! for a string with multiple words.
For example my code for city is:

print "What city are you from?
city = gets.chomp
city = city.capitalize!

Let’s say the user’s input is “san francisco”.

It seems that output for ‘city.capitalize!’ only returns “San francisco” and isn’t able to capitalize the ‘f’ in this case.

This thread on SO gives two approaches…

Give this one a try…

def capitalize_words(string)
  string.gsub(/\S+/, &:capitalize)
end
puts capitalize_words("HOW EXACTLY DO WE KNOW IF IT IS TO BE TRUE/FALSE?")
# How Exactly Do We Know If It Is To Be True/false?

Be sure to read the whole thread or you may miss important considerations.

1 Like

Instead of making a new variable as in the e.g. you add .the extra command to the command you made already .As in adding capitalize! to gets.chomp
e.g.
first_name = gets .chomp .capitalize!

I hope that helps

1 Like

What’s the reason for including the ! at the end of the method .captilize? Codecademy’s explanation didn’t really make sense.

1 Like

Is there a way in Ruby that can help us figure out capitalizing every first letter in each independent sentence if at some point we wanted the user to type a paragraph so that we will be sure that the program will take care of it in case the user messed something up?

I mean, is there some method like capitalize but capitalizes every letter at the beginning of each new sentence not just the very first one? If yes, how could Ruby know when the sentence begins and when it ends?

I am thinking of writing a snippet manually to do it, like:
“Hey, Ruby! If you see any (., ! or ?), please capitalize the following letter.”

This idea came to me just while writing this inquiry, but don’t know clearly how I am gonna do it.
I’m gonna give it a shot!

Hi, I thought so. So is this correct? I know there can be a reason for codeaccademy to suggest the other way, but I can’t think of any really :slight_smile:

I am still confused with capitalize and capitalize!
What makes it so different with exclamation sign. could anyone please explain it in simple words.

I have tried these code and works fine as well.
print “what is your first name?”
first_name = gets.chomp.capitalize
print “what is your last name?”
last_name = gets.chomp.capitalize
print"What is your city?"
city = gets.chomp.capitalize
print "What is your state? eg: NY, DC "
state = gets.chomp.upcase
puts “Dear #{first_name} #{last_name} ! You live in #{city}, #{state}!”

1 Like

first_name.capitalize doesn’t change the value of the first_name variable (so it doesn’t capitalize the value the user has input).

first_name.capitalize! does change the value of the first_name variable (so it does capitalize the value input by the user).

You can see the results of this in my code and output below (pay attention to the first_name variable for the .capitalize example. All other variables are .capitalize! apart from the state one).

Code:

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 "Which city do you live in?"
city = gets.chomp
city.capitalize!
print "Which state are you in (use abbreviation)?"
state = gets.chomp
state.upcase!
puts "Your name is #{first_name} #{last_name}, you live in #{city} in #{state} state"

Output:

What's your first name?david
What's your last name?smith
Which city do you live in?chicago
Which state are you in (use abbreviation)?il
Your name is david Smith, you live in Chicago in IL state

So you can see in the final sentence above that the first_name variable has not been capitalized (david) but the other variables have (Smith and Chicago).

I hope this makes sense to you. Mods - hope this is accurate, I wanted to further my own understanding by answering the question. Please correct anything I’m wrong on.

2 Likes

Thank you very much for your clear explanation.

I don’t see the point of adding answer2 either.

1 Like

Exactly! Which means that in the exercise if the user capitalizes correctly when your print out the variables you will get blanks, eg:

What is your first name? Andy
What is your last name? smith
Which city do you live in? denver
Please enter the two letter code for the state that city is in: CO
Your name is Smith and you live in Denver, .