Monday 9 December 2013

Learn Ruby Language and Be an exploit coder-14

Prompting And Passing.

Let's do one exercise that uses ARGV and gets.chomp() together to ask the user something specific. You will need this for the next exercise where we learn to read and write files. In this exercise we'll print a simple > prompt. This is similar to a game like Zork or Adventure.


  • user = ARGV.first
  • prompt = '> '

  • puts "Hi #{user}, I'm the #{$0} script."
  • puts "I'd like to ask you a few questions."
  • puts "Do you like me #{user}?"
  • print prompt
  • likes = STDIN.gets.chomp()

  • puts "Where do you live #{user}?"
  • print prompt
  • lives = STDIN.gets.chomp()

  • puts "What kind of co. 
  • mputer do you have?"
  • print prompt
  • computer = STDIN.gets.chomp()

  • puts <<MESSAGE
  • Alright, so you said #{likes} about liking me.
  • You live in #{lives}.  Not sure where that is.
  • And you have a #{computer} computer.  Nice.
  • MESSAGE

Important: Also notice that we're using STDIN.gets instead of plain 'ol gets. That is because if there is stuff in ARGV, the default gets method tries to treat the first one as a file and read from that. To read from the user's input (i.e., stdin) in such a situation, you have to use it STDIN.gets explicitly.

What You Should See.

When you run this, remember that you have to give the script your name for the ARGV arguments.

  • $ ruby exploit14.rb stoker
  • Hi Zed, I'm the exploit/exploit14.rb script.
  • I'd like to ask you a few questions.
  • Do you like me Stoker?
  • > Yes
  • Where do you live Stoker?
  • > Pakistan
  • What kind of computer do you have?
  • > Lenovo
  • Alright, so you said Yes about liking me.
  • You live in Pakistan.  Not sure where that is.
  • And you have a Lenovo Laptop.  Nice.

Extra Credit

Find out what Zork and Adventure were. Try to find a copy and play it.
Change the prompt variable to something else entirely.
Add another argument and use it in your script.
Make sure you understand how I combined a <<SOMETHING style multi-line string with #{ } string interpolation as the last print. 

No comments:

Post a Comment