Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为无效输入创建错误消息_Ruby_Date_Error Messaging - Fatal编程技术网

Ruby 为无效输入创建错误消息

Ruby 为无效输入创建错误消息,ruby,date,error-messaging,Ruby,Date,Error Messaging,我有下面的代码返回任意给定月份的天数,除非有人键入了非日期的内容,或者他们的日期格式错误,否则该代码可以正常工作。为了解决这个问题,我想发送一个无效输入的错误消息,但我不知道怎么做。那么,如何为这个小应用程序创建错误消息呢 #type in the month and year you want like so ---> "Feb 2034" require 'date' input = gets.chomp inputArray = input.split(" ").to_a

我有下面的代码返回任意给定月份的天数,除非有人键入了非日期的内容,或者他们的日期格式错误,否则该代码可以正常工作。为了解决这个问题,我想发送一个无效输入的错误消息,但我不知道怎么做。那么,如何为这个小应用程序创建错误消息呢

   #type in the month and year you want like so ---> "Feb 2034"

require 'date'

input = gets.chomp

inputArray = input.split(" ").to_a

textMonth = inputArray[0]
textYear = inputArray[1]

startOfMonth = Date.strptime(input, "%b %Y")
nextMonth = startOfMonth.next_month
endOfMonth = nextMonth - 1
daysInMonth = (endOfMonth - startOfMonth + 1).to_i

puts "#{textMonth} of year #{textYear} has #{daysInMonth} days!"

也许最好的方法是将您的输入放入while循环,每当输入与您期望的不同时,都会提示您输入新的答案

要检查输入,应使用。这是一个
如何编写与日期匹配的regexp。

最好的方法可能是将输入放入while循环中,每次输入与预期不符时都提示输入新答案

要检查输入,应使用。这是一个
如何编写与日期匹配的regexp。

有关创建自定义错误的信息,请参阅以下代码: 在这里,我为错误的日期输入创建并引发
InvalidDateError

   #type in the month and year you want like so ---> "Feb 2034"
class InvalidDateError < StandardError
end

require 'date'
require 'pry-byebug'
input = gets.chomp

inputArray = input.split(" ").to_a

textMonth = inputArray[0] 
textYear = inputArray[1]
begin
    startOfMonth = Date.strptime(input, "%b %Y")
    nextMonth = startOfMonth.next_month
    endOfMonth = nextMonth - 1
    daysInMonth = (endOfMonth - startOfMonth + 1).to_i
    puts "#{textMonth} of year #{textYear} has #{daysInMonth} days!"
rescue StandardError=> e
    raise InvalidDateError.new("Invalid Date : #{input}")
end
#输入您想要的月份和年份,如--->“2034年2月”
类InvalidDateErrore
raise INVALIDDATERROR.new(“无效日期:#{input}”)
结束
如果您不想引发错误,只想显示错误消息,请替换
raiseinvalidDateError.new(“无效日期:{input}”)

使用
放置“无效日期:{input}”

要创建自定义错误,请参阅以下代码: 在这里,我为错误的日期输入创建并引发
InvalidDateError

   #type in the month and year you want like so ---> "Feb 2034"
class InvalidDateError < StandardError
end

require 'date'
require 'pry-byebug'
input = gets.chomp

inputArray = input.split(" ").to_a

textMonth = inputArray[0] 
textYear = inputArray[1]
begin
    startOfMonth = Date.strptime(input, "%b %Y")
    nextMonth = startOfMonth.next_month
    endOfMonth = nextMonth - 1
    daysInMonth = (endOfMonth - startOfMonth + 1).to_i
    puts "#{textMonth} of year #{textYear} has #{daysInMonth} days!"
rescue StandardError=> e
    raise InvalidDateError.new("Invalid Date : #{input}")
end
#输入您想要的月份和年份,如--->“2034年2月”
类InvalidDateErrore
raise INVALIDDATERROR.new(“无效日期:#{input}”)
结束
如果您不想引发错误,只想显示错误消息,请替换
raiseinvalidDateError.new(“无效日期:{input}”)

使用Viktor建议的
将“无效日期:#{input}”
,从


正如维克多建议的那样,从


Ryan最好创建一个异常。只需扩展StandardError,创建您自己的错误类,然后用详细信息引发异常。我是一个彻头彻尾的noob,您能键入一个代码示例吗?Ryan最好创建一个异常。只需扩展StandardError,创建您自己的错误类,然后引发一个带有详细信息的异常。我是一个彻头彻尾的noob,您可以键入一个代码示例吗?那么我如何将此注释中的代码添加到我的帖子---->def valid_date(str,format=“%m/%d/%Y”)date.strtime(str,format)rescue false end那么我如何将此注释中的代码添加到我的帖子上面的代码--->def valid_date(str,format=“%m/%d/%Y”)date.strtime(str,format)rescue false end