• Stars
    star
    355
  • Rank 119,764 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 14 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

return the sourcecode for a method

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).

  • Some Ruby 1.8 support now available.
  • Support for MRI, RBX, JRuby, REE

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:

  • Occasional strange behaviour in Ruby 1.8
  • Cannot return source for C methods.
  • Cannot return source for dynamically defined methods.

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.

More Repositories

1

binding_of_caller

Retrieve the binding of a method's caller in MRI 1.9.2+
Ruby
661
star
2

plymouth

Start Pry in the context of a failed test
Ruby
178
star
3

funkify

Haskell-style partial application and composition for Ruby methods
Ruby
153
star
4

texplay

image manipulation tool for ruby and gosu
C
66
star
5

devil

ruby bindings for devil cross platform image loading library
Ruby
60
star
6

debug_inspector

A Ruby wrapper for the MRI 2.0 debug_inspector API
Ruby
58
star
7

free

force immediate garbage collection of an object
C
36
star
8

remix

Ruby modules re-mixed and remastered
Ruby
32
star
9

include_complete

Fixing the limitations in traditional Module#include
Ruby
19
star
10

pry-stack_explorer

MOVED TO https://github.com/pry/pry-stack_explorer
Ruby
18
star
11

prepend

prepends modules in front of a class; so method lookup starts with the module
C
16
star
12

selene

ruby spaceship game
Ruby
13
star
13

tremolo

Worms-like game for Ruby
Ruby
10
star
14

method_reload

fine grained code reloading
Ruby
10
star
15

state-ology

Clean and fast Object state transitions in Ruby using the Mixology C extension
Ruby
9
star
16

object2module

Convert Classes and Objects to Modules so they can be extended/included.
Ruby
9
star
17

pry-doc

Provide MRI Core documentation and source code for the Pry REPL
Ruby
9
star
18

mixology19

A version of the Mixology C extension that is 1.9.1 and 1.8.6 compatible
Ruby
8
star
19

pry-exception_explorer

MOVED TO https://github.com/pry/pry-exception_explorer
Ruby
7
star
20

gen_eval

a strange new breed of instance_eval
C
6
star
21

tweak

enhance classes with temporary functionality
Ruby
4
star
22

compat

Managing Ruby 1.9 and 1.8 C extension compatibility in one header file
C
3
star
23

custom_boolean

a hack to have if/else_if/else conditions with user-defined truthiness
Ruby
3
star
24

dup_eval

A souped up version of instance_eval in the vein of mix_eval
Ruby
3
star
25

pry-note

Ease refactoring and exploration by attaching notes to methods and classes in Pry"
Ruby
3
star
26

strange_evals

a hodgepodge of alternatives to instance_eval
Ruby
3
star
27

local_eval

instance_eval without changing self
Ruby
3
star
28

mult

a few utility functions written in C
C
2
star
29

basis

personal system setup for new *nix boxes
Shell
2
star
30

change_class2

change class of an object
Ruby
2
star
31

window-rotate-for-emacs

rotate the buffers for the active windows in emacs
2
star
32

pry_time

pry error console
Ruby
1
star
33

RailsExceptionExamples

Pry's @banisterfiend wanted a sample rails application to use for development… This is it with documentation.
Ruby
1
star
34

raise_shim

A shim for rb_raise
C
1
star
35

rumi

magic
C++
1
star
36

exercise

learning objc
Objective-C
1
star
37

baseline

organize project benchmarks into contexts
Ruby
1
star