1

I am under:

ruby-1.9.3-p194
Rails 3.0.9

I would like to use in my view method percent_of with Numeric data type.

For example, <%= 10.persent_of(50) %> it's equal 20%.

to do that I have created lib/numeric.rb file that contains

class Numeric
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end

But I got an error:

undefined method `percent_of' for 10:Fixnum

I was trying to add definition require 'Numeric' but it didn't help me.

Help me plz.

1
  • 2
    You may want to put your file in config/initializers instead. That way you don't need to require it. Commented Aug 2, 2012 at 9:03

2 Answers 2

1

Try require 'numeric' instead. You're requiring the file, not the class.

Sign up to request clarification or add additional context in comments.

Comments

1

Check whether you have config.autoload_paths += %W(#{config.root}/lib) in your application.rb file. You should extend Fixnum instead of Numeric:

class Fixnum
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end

Then require or include this in application_helper.rb so that it is available to all views:

require 'numeric' or include Numeric

Comments

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.