如何使用Ruby访问条件测试中的数组索引值

时间:2022-02-06 21:20:56

Background: I am trying to write a simple function that generates a list of calendar days, which I have mostly working except for one if/else loop.

背景:我正在尝试编写一个简单的函数,它生成一个日历天数列表,除了一个if/else循环外,我大部分时间都在工作。

The relevant variables and their initial declaration values:

相关变量及其初始声明值:

monthsOfYear = %w[January February March April May June July August September October November December]
currentMonthName = "" # empty string
daysInMonth = 0 # integer

The relevant loop:

相关的循环:

monthsOfYear.each do |month| #loop through each month
    # first get the current month name
    currentMonthName = "#{month}" # reads month name from monthsOfYear array
    if ??month == 3 || 5 || 8 || 10 ?? # April, June, September, November 
        daysInMonth = 30
    elsif ??month == 1?? # February
        if isLeapYear
            daysInMonth = 29
        else
            daysInMonth = 28
        end
    else # All the rest
        daysInMonth = 31
    end

I've marked the part that I'm having trouble with between ?? ?? Basically, I am trying to figure out how to access the numerical value of the index as it loops and test whether that index number matches the few specific cases. I have searched the documentation extensively trying to find a method that returns the index number value (NOT the value stored at x index), in other words I want to be able to read x in Array[x], not what's stored at Array[x]

我把中间有麻烦的部分标出来了?? ?基本上,我正在尝试找出如何在索引循环时访问它的数值,并测试该索引号是否与少数特定情况匹配。我搜索了大量的文档,试图找到一个方法来返回索引号值(而不是存储在x索引处的值),换句话说,我希望能够在数组[x]中读取x,而不是在数组[x]中存储的值

Perhaps in this specific case it would be better to test whether month == "April" || "June" || "September" || "November" rather than trying to build the case from parsing the array index number?

也许在这个特定的情况下,测试month = "April" || "June" || "September" || "November"是否更好,而不是试图通过解析数组索引号来构建case ?

But in general, what method can be called to find out the index number value? Or is that even possible?

但是一般来说,可以调用什么方法来查找索引号值呢?或者这可能吗?

2 个解决方案

#1


2  

To get the index of an array item, use the index method:

要获取数组项的索引,请使用index方法:

monthsOfYear = [ "January", "February", "March", ... ]
monthsOfYear.index("February") #=> 1

If you're looking for date calculations specifically, Ruby has a built-in way:

如果您正在寻找具体的日期计算,Ruby有一个内置的方法:

Date.new(date.year, date.month, -1).mday #=> the number of days in the month

If you're looking to iterate with the month and index, Anthony's answer is correct.

如果你想对月份和指数进行迭代,安东尼的答案是正确的。

monthsOfYear.each_with_index do |month, index| {
  ...
  # The first loop: month = "January", index = 0
  ...
}

If you're looking for a way to improve your code, use a case statement:

如果您正在寻找改进代码的方法,请使用case语句:

case month
when "April", "June", "September", "November" 
  daysInMonth = 30
when "February"
  if isLeapYear
    daysInMonth = 29
  else
    daysInMonth = 28
  end
else
  daysInMonth = 31
end

In Ruby, you can set anything equal to the result of a case statement, and also a case statement can match numbers, so it's possible to write:

在Ruby中,您可以设置任何与case语句的结果相等的内容,而且case语句可以匹配数字,因此可以写:

daysInMonth = case monthsOfYear.index(currentMonthName) 
when 3, 5, 8, 10 
  30
when 1
  isLeapYear ? 29 : 28
else
  31
end

#2


5  

Joel's answer is a better implementation but to keep with your code and to answer your question, Enumerable has an an each_with_index method (Enumberable#each_with_index):

Joel的答案是一个更好的实现,但是为了保留代码并回答您的问题,Enumerable有一个each_with_index方法(Enumberable#each_with_index):

monthsOfYear.each_with_index do |month, index|

then you can use index in your if/else conditionals. Note that arrays are zero based so January is actually going to be 0.

然后可以在if/else条件句中使用索引。注意数组是基于0的,所以1月份实际上是0。

#1


2  

To get the index of an array item, use the index method:

要获取数组项的索引,请使用index方法:

monthsOfYear = [ "January", "February", "March", ... ]
monthsOfYear.index("February") #=> 1

If you're looking for date calculations specifically, Ruby has a built-in way:

如果您正在寻找具体的日期计算,Ruby有一个内置的方法:

Date.new(date.year, date.month, -1).mday #=> the number of days in the month

If you're looking to iterate with the month and index, Anthony's answer is correct.

如果你想对月份和指数进行迭代,安东尼的答案是正确的。

monthsOfYear.each_with_index do |month, index| {
  ...
  # The first loop: month = "January", index = 0
  ...
}

If you're looking for a way to improve your code, use a case statement:

如果您正在寻找改进代码的方法,请使用case语句:

case month
when "April", "June", "September", "November" 
  daysInMonth = 30
when "February"
  if isLeapYear
    daysInMonth = 29
  else
    daysInMonth = 28
  end
else
  daysInMonth = 31
end

In Ruby, you can set anything equal to the result of a case statement, and also a case statement can match numbers, so it's possible to write:

在Ruby中,您可以设置任何与case语句的结果相等的内容,而且case语句可以匹配数字,因此可以写:

daysInMonth = case monthsOfYear.index(currentMonthName) 
when 3, 5, 8, 10 
  30
when 1
  isLeapYear ? 29 : 28
else
  31
end

#2


5  

Joel's answer is a better implementation but to keep with your code and to answer your question, Enumerable has an an each_with_index method (Enumberable#each_with_index):

Joel的答案是一个更好的实现,但是为了保留代码并回答您的问题,Enumerable有一个each_with_index方法(Enumberable#each_with_index):

monthsOfYear.each_with_index do |month, index|

then you can use index in your if/else conditionals. Note that arrays are zero based so January is actually going to be 0.

然后可以在if/else条件句中使用索引。注意数组是基于0的,所以1月份实际上是0。