When you're just getting familiar with Ruby, it can be overwhelming trying to figure out where methods are defined. A given method could have been defined by your own app, a gem, or by Ruby itself.
For example, an array responds to from
:
['a', 'b', 'c'].from(1)
=> ["b", "c"]
Where is from
from? Use the method
method!
['a', 'b', 'c'].method(:from)
=> #<Method: Array#from(position) ~/.rvm/gems/ruby-2.7.2/gems/activesupport-6.1.4/lib/active_support/core_ext/array/access.rb:12>
from
is added into Array by ActiveSupport.
For core Ruby methods, calling method
won't point you to the source code. But the name that is provided is usually enough to get you to the right documentation.
[].method(:pop)
=> #<Method: Array#pop(*)>
pop
is a part of the Array class.
[].method(:detect)
=> #<Method: Array(Enumerable)#detect(*)>
detect
is a part of the Enumerable module that is included in Array.
If a method is just an alias
for another method, then method
isn't going to help us figure out where that alias was defined:
[].method(:blank?)
=> #<Method: Array#blank?(empty?)()>
In this case, we just have to search through ActiveSupport to see that it sets up blank?
as an alias for empty?
.