private method 'save' called for #<Restaurant: 0x00000001619d00> (NoMethodError)

154 views Asked by At

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.

1

There are 1 answers

0
super_J On

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.