Erik V
Erik V

Reputation: 385

Abstracted way to find a method definition in Rails

Background:

I have been researching the Rails method number_to_currency, trying to find what units are available for the currency and how to pass them to the options block. Going on both APIDock and Ruby on Rails API, and searching the Rails Repository, I have not found the actual method definition. I have also tried to call the source_location method on number_to_currency but this returned an error.

Question:

I am looking for an abstracted way of finding any RoR method definition.

Upvotes: 1

Views: 35

Answers (1)

Axel Tetzlaff
Axel Tetzlaff

Reputation: 1364

Every ruby object has a method named method. This method-method can be passed name and it will return a method-object containing information about the method with the given name. This includes information about the source_location of the method as well.

Methods like number_to_currencyare helper methods defined on the view_context object on which your views are exected on.

See this thread on how to execute these on the console.

There you can then examine these methods like this:

irb(main):001:0> helper.number_to_currency 3.14
=> "3,14 €"
irb(main):002:0> helper.method(:number_to_currency)
=> #<Method: ActionView::Base(ActionView::Helpers::NumberHelper)#number_to_currency>
irb(main):003:0> helper.method(:number_to_currency).source_location
=> ["/Users/at/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionview-4.1.5/lib/action_view/helpers/number_helper.rb", 107]

Upvotes: 1

Related Questions