How can I use the rails helper "distance_of_time_in_words" in plain old ruby (non-rails) How can I use the rails helper "distance_of_time_in_words" in plain old ruby (non-rails) ruby ruby

How can I use the rails helper "distance_of_time_in_words" in plain old ruby (non-rails)


This should work:

require 'action_view'

include ActionView::Helpers::DateHelper

Both of these need to be done for a couple of reasons. First, you need to require the library, so that its modules and methods are available to be called. This is why you need to do require 'action_view'.

Second, since distance_of_time_in_words is module, which does not stand on its own, it needs to be included in class. You can then access it by calling distance_of_time_in_words on an instance of that class.

When you are in the console, you already have an instance of the Object class running. You can verify this by calling self in the irb console. When you call include ActionView::Helpers::DateHelper, you are including those methods for any instance of the Object class. Since that is the implicit receiver of the irb console, you can then just save distance_of_time_in_words right on the console and get what you want!

Hope that helps.

Joe