Getting a private method error in Ruby on Rails

I have a controller:

class StatsController < ApplicationController require 'time' def index @started = "Thu Feb 04 16:12:09 UTC 2010" @finished = "Thu Feb 04 16:13:44 UTC 2010" @duration_time = stats_duration(@started, @finished) end private def stats_duration(started, finished) time_taken = distance_of_time_in_words(Time.parse(started), Time.parse(finished)) time_taken end end 

It takes time to start and end and calculates the duration between times.

When I run this, I get the following error:

private method `gsub! 'Called Tu Feb 04 16:12:09 UTC 2010: Time

Why is this happening?

+6
ruby ruby-on-rails
source share
1 answer

private gsub method! called Time.parse when used, it usually means that you called parse with a Time object, not a String , so it seems like your code is actually trying to parse time twice.

eg.

 >> t = Time.now => Fri Feb 05 13:12:17 +0000 2010 >> Time.parse(t) NoMethodError: private method `gsub!' called for Fri Feb 05 13:12:17 +0000 2010:Time from c:/ruby/lib/ruby/1.8/date/format.rb:965:in `_parse' from c:/ruby/lib/ruby/1.8/time.rb:240:in `parse' from (irb):6 
+12
source share

All Articles