接收ruby中未定义的方法错误(无rails)
我真的是新手,所以我为我的无知道歉,我在询问之前已经搜索了资源 我使用的是常规ruby,我使用的是API 当我运行东西时,我不断收到未定义的方法错误,我无法找出原因 这是我的代码。。。。问题是我认为最后两种方法。。。但我不明白是什么原因导致我调用的方法“打印时间”被认为是未定义的。其他资源指出,这通常是一个对象的问题,但我想这对我来说没有意义 以下是CLI不工作的代码接收ruby中未定义的方法错误(无rails),ruby,undefined,Ruby,Undefined,我真的是新手,所以我为我的无知道歉,我在询问之前已经搜索了资源 我使用的是常规ruby,我使用的是API 当我运行东西时,我不断收到未定义的方法错误,我无法找出原因 这是我的代码。。。。问题是我认为最后两种方法。。。但我不明白是什么原因导致我调用的方法“打印时间”被认为是未定义的。其他资源指出,这通常是一个对象的问题,但我想这对我来说没有意义 以下是CLI不工作的代码 class Cli def start puts "Shabbat Shalom!"
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
这是烛光课程的代码
class CandleLighting
attr_accessor :title, :date
@@all = []
def initialize(title, date)
@title = title
@date = date
@@all << self
end
def self.all
@@all
end
end
最后是与CLI第52行相关的错误消息,该行为“打印时间(日期)”
回溯(最近一次呼叫最后一次):
6:从bin/运行:4:在`'
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':未定义的方法'print_lighting_time'(命名错误)
我不确定是否所有这些代码都是必要的,以帮助…但我一直试图修复这一问题相当长的一段时间,它没有发生
提前谢谢!将此内容放入代码编辑器并正确缩进它会发现问题。
打印照明时间
在照明时间信息
中定义
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
应该是
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
缩进是防止此类错误的重要视觉指南
一个好的编辑器喜欢或会为您缩进,并可以警告您常见错误。诸如之类的工具会检查您的代码是否存在常见错误。您将Cli的代码粘贴了两次,而不是CandleLighting类。您能显示实际的错误消息以及它发生在哪一行吗?@schwern非常感谢。我修复了它并添加了错误或留言!非常感谢。我刚刚学到了一个关于如何缩进的非常宝贵的教训。天哪。非常感谢你。@AliceSun缩进并不是唯一挑剔的人。:)很高兴你把它整理好了。祝你好运。
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
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