Alice Sun
Alice Sun

Reputation: 73

Receiving undefined method error in ruby (no rails)

I am really new to this so I apologize for my ignorance and I have searched for resources before asking.

I am using regular ruby and I am using an API.

I keep receiving undefined method error when I run things and I cannot figure out why.

So, this is my code.... the issue is the last two methods I think... but I don't understand what it is that is cause the method I am calling #print_lighting_time to come up as undefined. Other resources have stated that it is usually an issue with an object but I guess that isn't making sense to me...

here is the code for the CLI that isn't working

class Cli
    def start 
       puts "Shabbat Shalom!"
       Api.get_data
       check_date_options
    end

def check_date_options
    puts "Curious when to light your candles for Shabbos? Type 'Dates' to find out!"
    check_date
end

    def check_date 
        input = get_input

            if input == "Dates"
                list_dates
            else 
                invalid_date  
            end
    end

    def get_input
        gets.chomp
      end 

    def invalid_date
        puts "Invalid date! Check your date and reenter!"
        binding.pry
    end

    def list_dates
          CandleLighting.all.each_with_index do |title, index|
            puts "#{index}. #{title.date}"
        end

        lighting_times_info_list
    end
    
  def lighting_times_info_list
        puts "Select the date to view the lighting time!" 
        lighting_times_info
    end

def lighting_times_info
    input = get_input
        
    if input.to_i.between?(0, 60)

        index = input.to_i 
       date = CandleLighting.all[index]
        print_lighting_time(date)
    else 
        invalid_date
        lighting_times_info_list
    end


def print_lighting_time(date)
    puts "Shabbos is:#{date}"
    puts "Light candles by: #{date.title}"
      
end 
end
end

and here is the code for the CandleLighting class

class CandleLighting

    attr_accessor :title, :date

    @@all = []

    def initialize(title, date)
        @title = title
        @date = date
        @@all << self
    end

    def self.all 
        @@all 
    end
end

and the code for the API

class Api
def self.get_data
    load_candlelightings
end

    def self.load_candlelightings
        response = RestClient.get("https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&min=on&mod=on&nx=on&year=now&month=x&ss=on&mf=on&c=on&geo=geoname&geonameid=5128581&m=50&s=on")
        data = JSON.parse(response.body)
        data["items"].each do |hash|
            CandleLighting.new(hash["title"], hash["date"]) if hash["title"].include?("Candle lighting")

        end  
    end
end 

and finally the error message that relates to line 52 of the CLI the line being "print_lighting_time(date)

Traceback (most recent call last):
        6: from bin/run:4:in `<main>'
        5: from /Users/allisonperry/Development/code/Mod1/candle-lighting-times/lib/cli.rb:5:in `start'
        4: from /Users/allisonperry/Development/code/Mod1/candle-lighting-times/lib/cli.rb:10:in `check_date_options'
        3: from /Users/allisonperry/Development/code/Mod1/candle-lighting-times/lib/cli.rb:17:in `check_date'
        2: from /Users/allisonperry/Development/code/Mod1/candle-lighting-times/lib/cli.rb:37:in `list_dates'
        1: from /Users/allisonperry/Development/code/Mod1/candle-lighting-times/lib/cli.rb:42:in `lighting_times_info_list'
/Users/allisonperry/Development/code/Mod1/candle-lighting-times/lib/cli.rb:52:in `lighting_times_info': undefined method `print_lighting_time' for #<Cli:0x00007fa94f883e48> (NoMethodError)

I am not sure if all this code is even necessary in order to help... but I have been trying to fix this for quite some time and its not happening.

Thank you in advance!

Upvotes: 1

Views: 1114

Answers (1)

Schwern
Schwern

Reputation: 165536

Putting this into a code editor and properly indenting it reveals the problem. print_lighting_time is defined inside lighting_times_info.

  def lighting_times_info
    input = get_input
    
    if input.to_i.between?(0, 60)
      
      index = input.to_i 
      date = CandleLighting.all[index]
      print_lighting_time(date)
    else 
      invalid_date
      lighting_times_info_list
    end
    
    
    def print_lighting_time(date)
      puts "Shabbos is:#{date}"
      puts "Light candles by: #{date.title}"
      
    end 
  end

It should instead be...

  def lighting_times_info
    input = get_input
    
    if input.to_i.between?(0, 60)
      index = input.to_i 
      date = CandleLighting.all[index]
      print_lighting_time(date)
    else 
      invalid_date
      lighting_times_info_list
    end
  end
    
  def print_lighting_time(date)
    puts "Shabbos is:#{date}"
    puts "Light candles by: #{date.title}"
  end 

Indentation is an important visual guide to prevent these sorts of mistakes.

A good editor like Atom or VSCode will indent for you and can warn you of common mistakes. Tools such as rubocop will audit your code for common mistakes.

Upvotes: 2

Related Questions