Newbie Question On Ruby Quiz

Discussion in 'Ruby' started by Kyle Murphy, Jun 30, 2008.

  1. Kyle Murphy

    Kyle Murphy Guest

    I'm a programming and Ruby newbie. I wanted to build some programs, so
    I started Best of Ruby Quiz.

    The first quiz is MadLibs (http://www.rubyquiz.com/quiz28.html) and,
    given the answer, I can't even get it to work.

    I have a ruby file with the answer in it:

    # use Ruby's standard template engine
    require "erb"

    # storage for keyed question reuse
    $answers = Hash.new

    # asks a madlib question and returns an answer
    def q_to_a( question )
    question.gsub!(/\s+/, " ") # normalize spacing

    if $answers.include? question # keyed question
    $answers[question]
    else # new question
    key = if question.sub!(/^\s*(.+?)\s*:\s*/, "") then $1 else nil end

    print "Give me #{question}: "
    answer = $stdin.gets.chomp

    $answers[key] = answer unless key.nil?

    answer
    end
    end

    # usage
    unless ARGV.size == 1 and test(?e, ARGV[0])
    puts "Usage: #{File.basename($PROGRAM_NAME)} MADLIB_FILE"
    exit
    end

    # load Madlib, with title
    madlib = "\n#{File.basename(ARGV.first, '.madlib').tr('_', ' ')}\n\n" +
    File.read(ARGV.first)
    # convert ((...)) to <%= q_to_a('...') %>
    madlib.gsub!(/\(\(\s*(.+?)\s*\)\)/, "<%= q_to_a('\\1') %>")
    # run template
    ERB.new(madlib).run



    And a .madlib file with (copied from the book):
    Our favorite language is ((gem:a gemstone)). We think ((gem)) is
    better than ((a gemstone)).



    Whenever I run the .rb file I get this error: Usage: madlib.rb
    MADLIB_FILE

    My question is basically: how do I make this program work? Thank you.
    --
    Posted via http://www.ruby-forum.com/.
     
    Kyle Murphy, Jun 30, 2008
    #1
    1. Advertising

  2. Kyle Murphy

    Sean Murphy Guest

    Newby question about Variables

    All,

    this is a very basic question about Variables and I wish to clear it up. I
    have not yet successfully mastered Object programming and still getting my
    head around the whole story.

    Global Variables are accessible anywhere within the program and are not
    recommended style of programming in OOPS.

    Local Variables are isolated to the Object or method.


    Instant Variables I am completely mystified on their purpose and their
    difference between a local variable.

    Sean
     
    Sean Murphy, Jun 30, 2008
    #2
    1. Advertising

  3. Kyle Murphy wrote:
    > I'm a programming and Ruby newbie. I wanted to build some programs, so
    > I started Best of Ruby Quiz.
    >
    > The first quiz is MadLibs (http://www.rubyquiz.com/quiz28.html) and,
    > given the answer, I can't even get it to work.
    >

    (...)
    >
    > # usage
    > unless ARGV.size == 1 and test(?e, ARGV[0])
    > puts "Usage: #{File.basename($PROGRAM_NAME)} MADLIB_FILE"
    > exit
    > end

    (..)
    >
    >
    > And a .madlib file with (copied from the book):
    > Our favorite language is ((gem:a gemstone)). We think ((gem)) is
    > better than ((a gemstone)).
    >
    >
    >
    > Whenever I run the .rb file I get this error: Usage: madlib.rb
    > MADLIB_FILE
    >
    > My question is basically: how do I make this program work? Thank you.


    It's trying to tell you that you're supossed to run madlib.rb with an
    argument, specifying which .madlib file to use.
    So don't start it with:

    madlib.rb

    use
    madlib.rb whatever_you_named_it.madlib

    instead. Hth,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Jun 30, 2008
    #3
  4. Re: Newby question about Variables

    Sean Murphy wrote:
    > Instant Variables I am completely mystified on their purpose and their
    > difference between a local variable.


    They're called instance variables, not instant variables. And they're called
    that because they're specific to an instance of a class (i.e. to an object).
    For example take the following class:
    class Foo
    def initialize(bar)
    @bar = bar
    end
    def bar
    @bar
    end
    end
    f1 = Foo.new(5)
    f2 = Foo.new(4)
    f1.bar #=> 5
    f2.bar #=> 4

    So here you have two instances of the class Foo. One instance has the value 5
    stored in the instance variable @bar, the other one the value 4. As long as
    f1 and f2 exist you will be able to access their values for @bar via the bar-
    method. If @bar were a local variable, it would disappear after the initialize
    method is done and thus would not be accessible from the bar method. If @bar
    was a global variable, it would not be object-specific, so f1.bar and f2.bar
    would return the same thing.

    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Jun 30, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruby Quiz

    [QUIZ] Animal Quiz (#15)

    Ruby Quiz, Jan 14, 2005, in forum: Ruby
    Replies:
    11
    Views:
    376
    James Edward Gray II
    Jan 18, 2005
  2. David Tran
    Replies:
    9
    Views:
    195
    David Tran
    Jan 21, 2005
  3. Ruby Quiz

    [QUIZ] 1-800-THE-QUIZ (#20)

    Ruby Quiz, Feb 18, 2005, in forum: Ruby
    Replies:
    15
    Views:
    330
    gabriele renzi
    Feb 24, 2005
  4. Marcelo Alvim

    [QUIZ] Newbie doubts about the quiz

    Marcelo Alvim, Aug 15, 2006, in forum: Ruby
    Replies:
    15
    Views:
    256
    Marcelo Alvim
    Aug 16, 2006
  5. Daniel Moore
    Replies:
    10
    Views:
    313
    James Gray
    Jan 31, 2009
Loading...

Share This Page