1

From following a tutorial, I have this instance method for class Guide, which has an inner class, class Config:

  def add
     puts "\nAdd a restaurant\n\n".upcase
     restaurant = Restaurant.new
     print "Restaurant name: "
     restaurant.name = gets.chomp.strip
     print "Cuisine type: "
     restaurant.cuisine = gets.chomp.strip
     print "Average price: "
     restaurant.price = gets.chomp.strip
     # An instance method on the Restaurant class that we'll save
     if restaurant.save
       puts "\nRestaurant Added\n\n"
     else
       puts "\nSave Error: Restaurant not added\n\n"
     end 
  end

Here is the Restaurant class code: `

class Restaurant

  @@filepath = nil

  def self.filepath=(path=nil)
        @@filepath = File.join(APP_ROOT, path)
  end
  attr_accessor :name, :cuisine, :price

  def self.file_exists?
        # class should know if a restaurant file file_exists
    if @@filepath && File.exists?(@@filepath)
      return true
    else
      return false      
    end
  end

  def self.file_usable?
    return false unless @@filepath
    return false unless File.exists?(@@filepath)
    return false unless File.readable?(@@filepath)
    return false unless File.writable?(@@filepath)
    return true
  end

  def self.create_file
        # create restaurant file
    File.open(@@filepath, 'w') unless file_exists?
    return file_usable?
  end      

  end

  def self.saved_restaurants
        # read the restaurant file
        # return instances of restaurant
  end

  def save
    return false unless Restaurant.file_usable?
    File.open(@@filepath, 'a') do |file|
      file.puts "#{[@name, @cuisine, @price].join("\t")}\n"
    end
    return true
  end

`

And I get this error saying I am calling a private method when I'm not:

➜  ruby_executables  cd restaurant_app
➜  restaurant_app git:(master) ✗ ruby ./init.rb
Found restaurant file


<<<< Welcome to the Food Finder >>>>

This is an interactive guide to help you find the food you crave.

(Enter response or type quit to exit)> list
Listing...
(Enter response or type quit to exit)> find
Finding...
(Enter response or type quit to exit)> add

ADD A RESTAURANT

Restaurant name: sams
Cuisine type: american
Average price: 12
/home/jacqueline/ruby_executables/restaurant_app/lib/guide.rb:97:in `add': private method `save' called for #<Restaurant:0x00000001619d00> (NoMethodError)
        from /home/jacqueline/ruby_executables/restaurant_app/lib/guide.rb:79:in `do_action'
        from /home/jacqueline/ruby_executables/restaurant_app/lib/guide.rb:54:in `launch!'
        from ./init.rb:29:in `<main>'
➜  restaurant_app git:(master) ✗

I don't understand why I am getting this error or what to do to fix it.

super_J
  • 234
  • 2
  • 10
  • 1
    You call `restaurant.save` in our code and `save` seems to be private (for whatever reason). Nobody will be able to help you if you do not show us all your code in your `Restaurant` class. – spickermann Nov 10 '14 at 05:56
  • Please show the Restaurant class. – rderoldan1 Nov 10 '14 at 15:20
  • Restaurant class code has been added as an edit to the above question, @spickermann – super_J Nov 11 '14 at 06:52
  • Restaurant class code has been added, @rderoldan1 – super_J Nov 11 '14 at 06:54
  • You have an extra `end` after your `create_file` method. It's likely related. – sevenseacat Nov 11 '14 at 07:11
  • The code looks fine, @JacquelineS.Homan, do you have the project in github? – rderoldan1 Nov 11 '14 at 13:54
  • Yes, I have it in my Github. But I think I found the problem even though the error message was VERY counter-intuitive. The `end` after the `create_file` method belonged at the end of the `class Restaurant` file and should have been there in the first place. Dunno how it wound up in the middle of my file instead (probably from my wrist bumping the mousepad). Anyhow, after fixing that and re-running the program, it works correctly now, @rdderoldan – super_J Nov 11 '14 at 16:55
  • It's VERY hard when you're struggling just trying to learn this stuff when you're new at it. Especially when you're a poor woman who never had the privilege of an advanced education and the ability to afford expensive courses and dev bootcamps. – super_J Nov 11 '14 at 17:09

1 Answers1

1

Apparently, a stray end after the create_file method, which should have been at the end of the class Restaurant file instead of in the middle of the class file after the def create_file block (probably due to my wrist bumping the mousepad while coding), was the source of the error. Removing it and putting it where it belonged in the first place—at the very end of the class Restaurant file—solved the problem as the program runs correctly now. But the error message itself was VERY counter-intuitive for me so I was not expecting to look for a syntax error like a stray end being out of place.

super_J
  • 234
  • 2
  • 10