Sunday, October 5, 2014

Ruby

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}!"

#=============================================================

  1. First we introduce one new method, capitalize, here. It capitalizes the first letter of a string and makes the rest of the letters lower case. We assign the result to answer2
  2. The next line might look a little strange, we don't assign the result ofcapitalize to a variable. Instead you might notice the ! at the end ofcapitalize. This modifies the value contained within the variable answeritself. The next time you use the variable answer you will get the results of answer.capitalize
  3. ===============================================================================
  4. print " what is your first name ? "
  5. first_name = gets.chomp
  6. first_name = first_name.capitalize!

  7. print " what is your last name ? "
  8. last_name = gets.chomp
  9. last_name = last_name.capitalize!

  10. print "what is your city ? "
  11. city = gets.chomp
  12. city = city.capitalize!

  13. print "what is your state ? "
  14. state = gets.chomp
  15. state = state.upcase!
  16. print " #{first_name} #{last_name} #{city} #{state} "
  17. =====================================================================



No comments: