Skip to content

ianmassey/method_source

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

method_source

(C) John Mair (banisterfiend) 2010

retrieve the sourcecode for a method

NOTE: This is nothing fancy; it simply utilizes Method#source_location in Ruby 1.9

method_source is a utility to return a method's sourcecode as a Ruby string. Also returns Proc and Lambda sourcecode.

It is written in pure Ruby (no C).

method_source provides the source method to the Method and UnboundMethod and Proc classes.

Example: methods

Set.instance_method(:merge).source.display
# =>
def merge(enum)
  if enum.instance_of?(self.class)
    @hash.update(enum.instance_variable_get(:@hash))
  else
    do_with_enum(enum) { |o| add(o) }
  end

  self
end

Limitations:

  • Only works with Ruby 1.9+ (YARV)
  • Cannot return source for C methods.
  • Cannot return source for dynamically defined methods.

Possible Applications:

About

return the sourcecode for a method

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors