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

#1

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!

#2

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
#3

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

#4

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!

#5

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?

#6

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.

4 Likes
#7

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

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

#8

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!
#9

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?

#10

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.

#11

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.

#12

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

#13

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