banister / method_source

return the sourcecode for a method
MIT License
361 stars 43 forks source link

method_source

Circle Build Status

(C) John Mair (banisterfiend) 2011

retrieve the sourcecode for a method

NOTE: This simply utilizes Method#source_location; it does not access the live AST.

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

Method comments can also be extracted using the comment method.

It is written in pure Ruby (no C).

method_source provides the source and comment methods to the Method and UnboundMethod and Proc classes.

Example: display method source

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

Example: display method comments

Set.instance_method(:merge).comment.display
# =>
# Merges the elements of the given enumerable object to the set and
# returns self.

Example: display module/class comments

MethodSource::MethodExtensions.method(:included).module_comment
# =>
# This module is to be included by `Method` and `UnboundMethod` and
# provides the `#source` functionality

Limitations:

Special Thanks

Adam Sanderson for comment functionality.

Dmitry Elastic for the brilliant Ruby 1.8 source_location hack.

Samuel Kadolph for the JRuby 1.8 source_location.

License

(The MIT License)

Copyright (c) 2011 John Mair (banisterfiend)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.