Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie Question On Ruby Quiz

Reply
Thread Tools

Newbie Question On Ruby Quiz

 
 
Kyle Murphy
Guest
Posts: n/a
 
      06-29-2008
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/.

 
Reply With Quote
 
 
 
 
Sean Murphy
Guest
Posts: n/a
 
      06-29-2008
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


 
Reply With Quote
 
 
 
 
Siep Korteling
Guest
Posts: n/a
 
      06-29-2008
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/.

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      06-30-2008
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[QUIZ] Gathering Ruby Quiz 2 Data (#189) Daniel Moore Ruby 10 01-31-2009 08:36 PM
[QUIZ] Newbie doubts about the quiz Marcelo Alvim Ruby 15 08-16-2006 02:08 PM
[QUIZ] 1-800-THE-QUIZ (#20) Ruby Quiz Ruby 15 02-24-2005 06:05 AM
[SOLUTION] Ruby Quiz #15 Animal Quiz David Tran Ruby 9 01-21-2005 02:11 AM
[QUIZ] Animal Quiz (#15) Ruby Quiz Ruby 11 01-18-2005 02:42 PM



Advertisments