Friday 6 December 2013

Learn Ruby Language and Be an exploit coder-5

More Variables And Printing

Helow readers!

Now we'll do even more typing of variables and printing them out. This time we'll use something called a "format string". Every time you put " (double-quotes) around a piece of text you have been making a string. A string is how you make something that your program might give to a human. You print them, save them to files, send them to web servers, all sorts of things.

Strings are really handy, so in this exercise you will learn how to make strings that have variables embedded in them. You embed variables inside a string by using specialized format sequences and then putting the variables at the end with a special syntax that tells Ruby, "Hey, this is a format string, put these variables in there."

As usual, just type this in even if you do not understand it and make it exactly the same.

  • my_name = 'stoker'
  • my_age = 35 # not a lie
  • my_height = 74 # inches
  • my_weight = 180 # lbs
  • my_eyes = 'Blue'
  • my_teeth = 'White'
  • my_hair = 'Brown'

  • puts "Let's talk about %s." % my_name
  • puts "He's %d inches tall." % my_height
  • puts "He's %d pounds heavy." % my_weight
  • puts "Actually that's not too heavy."
  • puts "He's got %s eyes and %s hair." % [my_eyes, my_hair]
  • puts "His teeth are usually %s depending on the coffee." % my_teeth

  • # this line is tricky, try to get it exactly right
  • puts "If I add %d, %d, and %d I get %d." % [
  •     my_age, my_height, my_weight, my_age + my_height + my_weight]


What You Should See.


  • $ ruby exploit5.rb
  • Let's talk about Stoker.
  • He's 74 inches tall.
  • He's 180 pounds heavy.
  • Actually that's not too heavy.
  • He's got Blue eyes and Brown hair.
  • His teeth are usually White depending on the coffee.
  • If I add 35, 74, and 180 I get 289.
  • $





Extra Credit


  1. Change all the variables so there isn't the my_ in front. Make sure you change the name everywhere, not just where you used = to set them.
  2. Try more format sequences.
  3. Search online for all of the Ruby format sequences.
  4. Try to write some variables that convert the inches and pounds to centimeters and kilos. Do not just type in the measurements. Work out the math in Ruby.


No comments:

Post a Comment