Ruby 为什么';";时间“自”;不为我工作?
为什么这对我不起作用:Ruby 为什么';";时间“自”;不为我工作?,ruby,Ruby,为什么这对我不起作用: class Time # Return the time difference (as a Float) between now and a specified older Time or parse-able String. # a = Time.now # Time.since(a) # => 8.920116 # a.since(a) # => 0.0 # Time.since '11:30' # => 28.1
class Time
# Return the time difference (as a Float) between now and a specified older Time or parse-able String.
# a = Time.now
# Time.since(a) # => 8.920116
# a.since(a) # => 0.0
# Time.since '11:30' # => 28.111561
# Time.since '9am' # => 9138.288258
def self.since(older_time)
Time.now.since(older_time)
end
def since(older_time)
self - (older_time.kind_of?(Time) ? older_time : Time.parse(older_time))
end
end
puts Time.since '9am'
错误消息:
.rb:13:insince':未定义的方法
parse'表示时间:类(NoMethodError)
from TimeSince.rb:9:insince'from
TimeSince.rb:17:in
'
你没有在它之前做
require'time'
,因此time.parse
方法(以及time
类中的所有其他方法)将不可用。你没有在它之前做require'time'
以及time.parse
方法(以及time
类中的所有其他方法)将不可用。从技术上讲,核心时间方法仍然可用(Time.new
,Time.now
,等等)——只是添加的标准库方法(Time.parse
是其中之一)需要require'Time'
语句。从技术上讲,核心时间方法仍然可用(Time.new
,Time.now
,等等)——只是添加了标准库方法(Time.parse
就是其中之一)才需要require'Time'
语句。