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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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}!”
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!
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!”
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?
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.
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!
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!
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}!”
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.
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, .