0
votes

I'm trying to run a code like this:

class Run_Fiction_Generator < Fiction_Generator
  def generate_story
    puts "Title: \"#{generator.title} #{generator.random_date}\""
    puts "---------"
    puts generator.story
  end
end

prompt = '>'

puts "Hello Welcome to Fiction Generator"
puts "First a little about me: "
puts ""

generator = Run_Fiction_Generator.new

puts "This amazing Fiction Generator was created by: #{generator.created_by}"
puts "He's a student with ID number: #{generator.student_id} "
puts ""
puts "Okay now your story! Enter '1' if you want to generate a story now, or '2' if you don't want to"
print prompt
answer = gets.chomp

if answer == "1"
  generate_story
else
  puts "It was nice meeting you, catch ya later!"
end

but the method generator_story in the if statement keeps throwing this error:

undefined local variable or method 'generate_story' for main:Object (NameError)

I'm under the impression that I already defined the method in my code. Can anyone tell where I've gone wrong?

3

3 Answers

6
votes

generate_story is a method of Run_Fiction_Generator and it should be called like this:

generator.generate_story
1
votes

Your other problem is how you're accessing your variables and methods inside the generate story method. Try this:

class Run_Fiction_Generator < Fiction_Generator
  def random_date
    "10/20/2012"
  end

  def title
   "Spooky, Late Halloween Story"
  end

  def generate_story
    puts "Title: \"#{title} #{random_date}\""
    puts "---------"
    puts @story
  end
end
0
votes

BTW: class name is "wrong"

use CamelCase for class names, not '_ to separate.