0

I would like to use the distance_of_time_in_words method in the date_helper.rb Rails file (see on Github) in an non-Rails Ruby project.

How can I include it? It requires other files, so how to include them?

I don't want to include all of Rails because that would slow down the development process.

Ruby 1.9.3

2 Answers 2

3

This method, distance_of_time_in_words is in actionpack/lib/action_view/helpers/date_helper.rb. So you should require 'action_view' and action_view/helpers to load this method. And the method is defined in module ActionView::Helpers::DateHelper, you can include it in your class. The method is an instance method.

require 'action_view'
require 'action_view/helpers'

class Klass
    include ActionView::Helpers::DateHelper
end

c = Klass.new
c.distance_of_time_in_words( ...... )    
Sign up to request clarification or add additional context in comments.

1 Comment

That's very helpful. I think the best answer is to include the required files without including Rails. But I gather that your answer uses the Rails gem.
2

If this is the only thing you want from it, then I'd just go take the source code and hack it to remove the dependencies (which appears to just be some I18n translations. To support the hack, you can probably translate this test suite.

Why would I do this instead of using the gem? Because it's just such an enormous dependency. It's so enormous that you actually notice it loading all that code. I'd rather rip out the method and hack it to work than depend on all of that (again, assuming this is the only thing you want from the lib).

1 Comment

That probably makes sense in this case because if is easy to remove the dependencies. But in general it might be more practical to include the required files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.