对问题的跟进 How to create a random time between a range .
Kernel#rand
适用于 Time
范围:
require 'time'
rand(Time.parse('9 am')..Time.parse('11:30 am'))
但是当我尝试使用自定义类时,我遇到了错误:
`rand': no implicit conversion of Range into Integer (TypeError)
class Int
include Comparable
attr_reader :num
def initialize(num)
@num = num
end
def succ
Int.new(num + 1)
end
def <=>(other)
num <=> other.num
end
def to_s
"Int(#{num})"
end
def to_int
@num
end
alias_method :inspect, :to_s
end
puts rand(Int.new(1)..Int.new(3))
为什么?我在自定义类(class)中缺少什么?我们可以在 rand(Range)
中使用这样一个自定义类吗?
最佳答案
我不知道关于 Kernel#rand
对 Range
参数的具体期望的任何文档,但我们可以通过覆盖 来了解发生了什么>respond_to?
在你的类里面,然后看着事情分崩离析:
def respond_to?(m)
puts "They want us to support #{m}"
super
end
这样做告诉我们 rand
想要在您的 Int
上调用 #-
和 #+
方法实例。考虑到 rand(a..b)
是为处理整数而设计的,这确实有一定道理。
因此我们引入了加法和减法的快速'n'dirty 实现:
def -(other)
self.class.new(to_int - other.to_int)
end
def +(other)
self.class.new(to_int + other.to_int)
end
我们开始从对 rand
的调用中获取 rand Int
。
我不确定在何处(或是否)对此进行了记录,因此请原谅您挥手致意。我通常会花一些时间研究 Ruby 源代码来回答这类问题,但我现在没有时间。
关于ruby - rand(Range) - 没有将 Range 隐式转换为 Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48292982/