Ruby -数组、边界和引发异常

时间:2022-06-06 20:18:29

Below is the code for my script.

下面是我的脚本代码。

As you can see, i have an array, and an index. I pass that to the block called 'raise_clean_exception'. The integer part of it does actually raise a Standard Error exception which is great. I have an issue when I use an index that is out of bounds. So if my array only has 4 elements (0-3) and I use an index of 9, it will not raise the exception, and instead it prints out a blank line because nothing is there. Why would it do this?

可以看到,我有一个数组和一个索引。我将它传递给名为“raise_clean_exception”的块。它的整数部分实际上提出了一个很好的标准错误异常。当我使用一个超出界限的索引时,我有一个问题。因此,如果我的数组只有4个元素(0-3),而我使用索引9,它不会引发异常,而是打印出一条空行,因为那里什么都没有。为什么会这样?

#!/usr/bin/ruby
puts "I will create a list for you.  Enter q to stop creating the list."
array = Array.new
i = 0
input = ''
print "Input a value: "  #get the value from the user
input = STDIN.gets.chomp
while input != 'q' do #keep going until user inputs 'q'
  array[i] = input  #store the value in an array of strings
  i += 1   #increment out index where the inputs are stored
  print "Input a value: "  #get the value from the user
  input = STDIN.gets.chomp
end  #'q' has been entered, exit the loop or go back through if not == 'q'

def raise_clean_exception(arr, index)
  begin
    Integer(index)
    puts "#{arr[index.to_i]}"
    # raise "That is an invalid index!"
  rescue StandardError  => e  # to know why I used this you can google Daniel Fone's article "Why you should never rescue exception in Ruby"
    puts "That is an invalid index!"
  end
  # puts "This is after the rescue block"
end

# now we need to access the array and print out results / error messages based upon the array index value given by the user
# index value of -1 is to quit, so we use this in our while loop
index = 0
arrBound = array.length.to_i - 1
while index != '-1' do
  print "Enter an index number between 0 and #{arrBound} or -1 to quit: "
  index = STDIN.gets.chomp
  if index == '-1'
    exit "Have a nice day!"
  end
  raise_clean_exception(array, index)
end

2 个解决方案

#1


4  

Accessing an array element that's outside the range of existing elements returns nil. That's just the way Ruby works.

访问超出现有元素范围的数组元素返回nil。这就是Ruby的工作方式。

You could add the following line before the "puts" to trap that condition...

您可以在“put”之前添加以下一行以捕获该条件……

raise StandardError if index.to_i >= arr.size

#2


7  

Consider using a subclass of StandardError, IndexError, which is specific to the problem you are experiencing. Also, using else prevents a blank space from being printed if the index is out of bounds and when raising exceptions within a method, a begin...end block is implied.

考虑使用StandardError的子类IndexError,它针对您正在遇到的问题。此外,如果索引超出范围,并且当在方法中引发异常时,使用else会阻止打印空白空间。块意味着结束。

def raise_clean_exception(arr, index)
  Integer(index)
  raise IndexError if index.to_i >= arr.length
  rescue StandardError
    puts "That is an invalid index!"
  else
    puts "#{arr[index.to_i]}"
end

#1


4  

Accessing an array element that's outside the range of existing elements returns nil. That's just the way Ruby works.

访问超出现有元素范围的数组元素返回nil。这就是Ruby的工作方式。

You could add the following line before the "puts" to trap that condition...

您可以在“put”之前添加以下一行以捕获该条件……

raise StandardError if index.to_i >= arr.size

#2


7  

Consider using a subclass of StandardError, IndexError, which is specific to the problem you are experiencing. Also, using else prevents a blank space from being printed if the index is out of bounds and when raising exceptions within a method, a begin...end block is implied.

考虑使用StandardError的子类IndexError,它针对您正在遇到的问题。此外,如果索引超出范围,并且当在方法中引发异常时,使用else会阻止打印空白空间。块意味着结束。

def raise_clean_exception(arr, index)
  Integer(index)
  raise IndexError if index.to_i >= arr.length
  rescue StandardError
    puts "That is an invalid index!"
  else
    puts "#{arr[index.to_i]}"
end