Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find source location of an aliased method

I have:

# lib/freedom_patches/html_safe.rb
class String
  alias_method :dangerously_output_as_html, :html_safe
end

Searching the method name dangerously_output_as_html in the codebase works but wondering is there a programmatic way to find out where this alias is?

"".method(:dangerously_output_as_html).source_location

brought me to the html_safe location:

activesupport/lib/active_support/core_ext/string/output_safety.rb

instead of where the monkey patch is:

lib/freedom_patches/html_safe.rb

Thanks in advance.

like image 671
Juanito Fatas Avatar asked Oct 28 '22 15:10

Juanito Fatas


1 Answers

I do not believe there is a way to arrive at the location where alias_method is used to define a method; I believe that under the hood at the C level, they share the same internal representation of a method. My grasp of the C code is far from complete, so I could be mistaken.

Instead, you may way to use

def dangerously_output_as_html
  html_safe
end
like image 89
meagar Avatar answered Oct 31 '22 08:10

meagar