Created
May 29, 2013 01:03
-
-
Save alexbartlow/5667286 to your computer and use it in GitHub Desktop.
Justin's Ruby Learning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Enter your name" | |
name = gets | |
length = name.length - 1 | |
print "Your name is " | |
print length | |
print " characters long." | |
# ok, so remember what we talked about with .chomp - whenever you use gets, you'll get the extra newline character on the end, | |
# (\n) - so chomp will remove that. That's why you have to subtract 1 to get the right length, so we can revise the code to the following | |
puts "Enter your name" | |
name = gets.chomp | |
length = name.length | |
print "Your name is " | |
print length | |
print " characters long." | |
# The other thing that's good to know is that you can use + to concatenate (join together) strings: | |
puts "Enter your name:" | |
name = gets.chomp | |
puts "Your name is " + length.to_s + " characters long." | |
# We use length.to_s to turn the Integer into a String. Most objects in ruby respond to to_s. | |
# This pattern, of putting data in the middle of a string, is so common, that there's a shortcut to do it:" | |
puts "Enter your name:" | |
puts "Your name is #{gets.chomp.length} characters long." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment