【问题标题】:Better way to search nested arrays in Ruby?在 Ruby 中搜索嵌套数组的更好方法?
【发布时间】:2018-10-21 07:27:06
【问题描述】:

对于上下文,我正在尝试解决这个问题。

  • 牙医预约时间表验证软件

  • 实现超类Appointment 和子类OneTimeDayMonth。一个Appointment 有一个description(例如,“根管”)和dates 信息(您可以使用Date 对象或int Year、Int Month、Int Day)。用各种约会填充Appointment 对象数组。

  • 在每个子类中编写一个方法 OccursOn,用于检查约会是否发生在该日期 (OneTime)、日期 (Day) 或月份 (Month)。要求用户输入要检查的日期(例如,2006 10 5),并询问用户是否要检查 OneTimeDayMonth 约会。根据用户选择的内容,每个子类中的 OccursOn 应该运行并显示任何匹配的约会和相关描述。

这是我目前所拥有的。

class Appointment
   attr_accessor :day, :month, :year, :info
   def initialize(day, month, year, info)
     @day = day
     @month = month
     @year = year
     @info = info
   end

   def occursOn

   end
end

class OneTime < Appointment
  def OneTime.occursOn(day, month, year)
    if @day.to_i == day.to_i && @month.to_i == month.to_i && @year.to_i == year.to_i
      puts "Good"
    else
      puts "Not Good"
    end
  end
end

class Day < Appointment
  def Day.occursOn(day)
    if @day.to_i == day.to_i

      puts "Good"
    else
      puts "Not Good"
    end
  end
end

class Month < Appointment
  def Month.occursOn(month)
    if @month.to_i == month.to_i
      puts "Good"
    else
      puts "Not Good"
    end
  end
end

app1 = OneTime.new("10", "11", "2018","Root Canal")
app2 = Day.new("10", "11", "2018", "Root Canal")
app3 = Month.new("10", "11", "2018", "Root Canal")
app4 = OneTime.new("11", "11", "2018", "Cleaning")
app5 = Day.new("11", "11", "2018", "Cleaning")
app6 = Month.new("11", "11", "2018", "Cleaning")
a = Array.new
a << app1 << app2 << app3 << app4 << app5 << app5 << app6



puts "Please enter the day of the appointment that you would like to search for"
day = gets.chomp
puts "Please enter the month of the appointment that you would like to search for"
month = gets.chomp
puts "Please enter the year of the appointment that you would like to search for"
year = gets.chomp


puts "Enter a number 1-3 to choose an answer out of OneTime, Day, or Month to search in that catagory respectivly"
answer = gets.chomp
if answer == "1"
  OneTime.occursOn(day, month, year)
elsif answer == "2"
  Day.occursOn(day)
elsif answer =="3"
  Month.occursOn(month)
else
  puts "Wrong answer"
end

我正在尝试验证与“日”、“月”和“年”相对应的用户输入与用户输入方法对应的每个数组值中的数字是否匹配。所以'OneTime.OccursOn' 应该只搜索由'OneTime.new' 组成的数组。我不能使用.include?因为日期和月份值相同的可能性。

这看起来很有用,但我不知道如何用我的子类实现这样的东西。

array = [
  ["A", "X"],
  ["B", "Y"],
  ["C", "Z"]
]


str = "Y"
arr = array.find{|a| a[1] == str}
puts arr[0] if arr
# => B

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 您的示例代码不起作用。您可以轻松确认。原因是,例如在class Day &lt; Appointment 中,变量a 是未定义的。因此,Day.OccursOn(day) 会引发 NameError。因此,您最好更新您的问题,并提供至少不会引发异常的代码。
  • 将其编辑为与您给出的答案类似的内容,第一篇文章中有 if 语句是我在测试问题的可能解决方案时留下的。

标签: ruby-on-rails arrays ruby


【解决方案1】:

这是实现您想要的示例代码(前提是我对您的意图的解释是正确的)。我使用方法名occursOn(注意在Ruby中,方法名的第一个字符应该是小写字母)。

class Day < Appointment
  def occursOn(day)
    if @day.to_i == day.to_i  # Compares as integers
      puts "Good"
    else
      puts "Not Good"
    end
  end
end

然后,一个样本测试:

app2 = Day.new("10", "11", "2018", "Root Canal")
app2.occursOn(10)   # => printing "Good"
app2.occursOn(999)  # => printing "not Good"

说明

使用实例方法,而不是像def Day.occursOn(day) 这样的类方法。 在Ruby中,当一个(实例)方法被调用时,该实例类中同名的方法优先级最高,因此其父类或祖先类中的同名方法被忽略。

这是 Ruby 的核心原则之一。我建议您通读 Ruby 教科书中的类部分,例如,Programming Ruby(免费在线)。

【讨论】:

  • 如果我使用(实例)方法而不是类方法,则会出现“Day:Class 的未定义方法 'occursOn'”。通过将 Day.occursOn(day) 更改为occursOn(day) 解决了这个问题,只是为了有一个“未定义的方法 'occursOn' for main:Object” 这一切都在代码底部的 if else 语句中。
  • 运行我的答案中的代码(Day 的定义),在运行你的class Appointment 的定义后,运行我的 3 行示例测试(包含在答案中)!它成功了。
猜你喜欢
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多