rubilicious

Learn Ruby the Hard Way

by Zed Shaw

Example 5

Previous   Next

# The assignment operator   =   assigns the string 'Jen Diamond'to the variable name
name = 'Jen Diamond'

# The assignment operator   =   assigns the integer 44 to the variable age
age = 44 # not a lie

# The assignment operator   =   assigns the integer 69 to the variable height
height = 69 # inches

# The assignment operator   =   assigns the integer 101 to the variable weight
weight = 101 # total lie

# The assignment operator   =   assigns the string 'green' to the variable eyes
eyes = 'green'

# The assignment operator   =   assigns the string 'white-ish' to the variable teeth
teeth = 'white-ish'

# The assignment operator   =   assigns the string 'auburn-ish' to the variable hair
hair = 'auburn-ish'

# String interpolation 
# %s and %d are formatters they are flags; 
# s stands for string  
# d stands for decimal number

# the method puts outputs the string and the embedded variable name
puts "Let's talk about %s." % name

# the method puts outputs the string and the embedded variable height
puts "She's %d inches tall." % height

# the method puts outputs the string and the embedded variable  
# which is an equation converting inches to centimeters using the variable of height in inches multiplied by 2.54
puts "She is also %d centimeters tall which is the same thing." % [height * 2.54]

# the method puts outputs the string and the embedded variable weight
puts "Today she weighs %d " % weight + "but is exercising and dieting to improve this number." 

# the method puts outputs the string and the embedded variable
# which is an equation converting pounds to kilograms using the variable of weight in pounds multiplied by 2.2
puts "Kilos sound better. Today she weighs %d kilos." % [weight / 2.2]

# the method puts outputs the string and the embedded variables for eyes and hair
puts "She has %s eyes and %s hair." % [eyes, hair]

# the method puts outputs the string and the embedded variable teeth
puts "Her teeth are usually %s depending on how much tea she has had." % teeth

# the method puts outputs the string and the embedded variables age, height, weight and the sum of these variables
puts "If I add %d, %d, and %d I get %d." % [age, height, weight, age + height + weight]