如何使用rails helper“distance_of_time_in_words”在普通的老红宝石(非轨道) [英] How can I use the rails helper "distance_of_time_in_words" in plain old ruby (non-rails)

查看:182
本文介绍了如何使用rails helper“distance_of_time_in_words”在普通的老红宝石(非轨道)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails有一个帮助器,可以使用distance_of_time_in_words转换单词中的时间距离



如果我只是在irb提示符下使用ruby来获取这个信息,那么需要包括什么功能?



我试过 require'action_view'但没有帮助。



我还尝试过 require'active_support / core_ext',但这并没有帮助。

解决方案

这应该是有效的:



require'action_view' / p>

include ActionView :: Helpers :: DateHelper



这两个都需要由于几个原因来完成。首先,您需要库,以便其模块和方法可以被调用。这就是为什么你需要做 require'action_view'



其次,因为 distance_of_time_in_words 是模块,它不能独立存在,它需要被包含在类中。然后,您可以通过在该类的实例上调用 distance_of_time_in_words 来访问它。



当您在控制台中时,您已经有一个运行 Object 类的实例。您可以通过调用irb控制台中的 self 来验证这一点。当您调用包含ActionView :: Helpers :: DateHelper 时,您将包含任何 Object 类的实例的方法。因为这是irb控制台的隐式接收器,您可以直接在控制台上保存 distance_of_time_in_words ,并获得所需的内容。



希望有帮助。



Joe


Rails has an helper which converts distance of time in words using distance_of_time_in_words

What do I need to include if I am just using ruby at the irb prompt to get this function?

I tried require 'action_view' but it didn't help.

I also tried require 'active_support/core_ext' but that didn't help either.

解决方案

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

这篇关于如何使用rails helper“distance_of_time_in_words”在普通的老红宝石(非轨道)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆