为什么“Time#since”对我不起作用?

时间:2011-06-02 23:00:50

标签: 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.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:在since': undefined method 解析'for Time:Class(NoMethodError)     来自TimeSince.rb:9:since' from TimeSince.rb:17:in'

1 个答案:

答案 0 :(得分:6)

您之前没有require 'time',因此Time.parse方法(以及Time类中的所有其他方法)都将无法使用。