23

I am looking to use ActionView::Helpers::NumberHelper from a Ruby script. What all do I need to require etc.?

2 Answers 2

37
~> irb
ruby-1.9.2-p180 :001 > require 'action_view'
 => true 
ruby-1.9.2-p180 :002 > ActionView::Base.new.number_to_currency 43
 => "$43.00" 
0
22

As of Rails 3.2.13, you can do the following:

class MyClass
  include ActionView::Helpers::NumberHelper

  def my_method
    ...
    number_with_precision(number, precision: 2)
    ...
  end
end

You might need to require 'action_view' too.

Edit: This answer is still valid in Rails 4.2.3.

2
  • 1
    require 'action_view' is what I was missing. Thank you.
    – Warpling
    Commented Feb 24, 2016 at 18:17
  • This was helpful. I think you can skip require 'action_view' if you're running the script via 'rails runner'
    – shirajg
    Commented Apr 13, 2018 at 18:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.