在Ruby中搜索嵌套数组的更好方法?

时间:2018-10-21 07:27:06

标签: ruby-on-rails arrays ruby

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

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

  • 实现超类Appointment和子类OneTimeDayMonthAppointment具有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

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

1 个答案:

答案 0 :(得分:0)

这里是实现您想要的示例代码(假设我对您的意图的解释是正确的)。我使用方法名称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(免费在线)。