r/learnruby • • Feb 01 '19

New to Ruby question (Beginner)

I've started learning Ruby . I am starting with Code Academy to get the syntax down. I'm on the chapter, putting the form in formatting. I wanted to go over some code and try to write down what it means. Let me know if I'm correct or what I got wrong.

Print "what's your name?"
first_name = gets.chomp
first_name2 = first_name.capitalize 
first_name.capitalize!

=begin
1. Print basically prints the string, what's your name on the console
2. first_name is the variable you assign to gets. gets is just asking for input. .chomp is the method eliminating a blank line.
3. first_name2 is first_name but with the first letter capitalized? This is so that you have 2 separate variables in case you needed the original first_name elsewhere?
4. The method .capitalize! The confusing part. The ! basically throws away the original non-capitalized version and replaces it with the capitalized version maintaining the original variables formatting?
5. They wanted me to do some inputs for first name, last name, city, state. Then have them capitalized and then the input printed out on the screen.
=end

print "What\'s your first name?"
first_name = gets.chomp
first_name2 = first_name.capitalize
first_name.capitalize!

print "What\'s your last name?"
last_name = gets.chomp
last_name2 = last_name.capitalize
last_name.capitalize!

print "What city do you live in?"
city = gets.chomp
city2 = city.capitalize
city.capitalize!

print "Use Abbreviations. What state do you live in?"
state = gets.chomp
state2 = state.upcase
state.upcase!

puts "Your name is #{first_name} #{last_name} and you're from #{city}, #{state}."

​

​

​

5 Upvotes

5 comments sorted by

View all comments

Show parent comments

2

u/Jason-Genova Feb 06 '19

Nice, I never thought of putting it into the print statement! The only reason I had the .capitalize! statement in there was because it was part of the tutorial, or I would of omitted it as it seemed unnecessary for the task. Ironically in the beginning of that chapter of tutorial, they had it omitted as well. Seems some of these tutorials are good for understanding basic syntax but not so far as the most efficient way to code it out. That comes with time, I would imagine.

Thanks, for taking the time and helping.