Module: MethodSource::MethodExtensions

Included in:
Method, Proc, UnboundMethod
Defined in:
lib/method_source.rb

Overview

This module is to be included by Method and UnboundMethod and provides the #source functionality

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

We use the included hook to patch Method#source on rubinius. We need to use the included hook as Rubinius defines a source on Method so including a module will have no effect (as it's higher up the MRO).

Parameters:

  • klass (Class)

    The class that includes the module.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/method_source.rb', line 84

def self.included(klass)
  if klass.method_defined?(:source) && Object.const_defined?(:RUBY_ENGINE) &&
      RUBY_ENGINE =~ /rbx/

    klass.class_eval do
      orig_source = instance_method(:source)

      define_method(:source) do
        begin
          super
        rescue
          orig_source.bind(self).call
        end
      end

    end
  end
end

Instance Method Details

#class_commentString Also known as: module_comment

Return the comments associated with the method class/module.

Examples:

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

Returns:

  • (String)

    The method's comments as a string

Raises:

  • SourceNotFoundException



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/method_source.rb', line 139

def class_comment
  if self.respond_to?(:receiver)
    class_inst_or_module =  self.receiver
  elsif self.respond_to?(:owner)
    class_inst_or_module = self.owner
  else
    return comment
  end

  if class_inst_or_module.respond_to?(:name)
    const_name = class_inst_or_module.name
  else
    const_name = class_inst_or_module.class.name
    class_inst_or_module = class_inst_or_module.class
  end

  location = class_inst_or_module.const_source_location(const_name)

  MethodSource.comment_helper(location, defined?(name) ? name : inspect)
end

#commentString

Return the comments associated with the method as a string.

Examples:

Set.instance_method(:clear).comment.display
=>
   # Removes all elements and returns self.

Returns:

  • (String)

    The method's comments as a string

Raises:

  • SourceNotFoundException



126
127
128
# File 'lib/method_source.rb', line 126

def comment
  MethodSource.comment_helper(source_location, defined?(name) ? name : inspect)
end

#sourceString

Return the sourcecode for the method as a string

Examples:

Set.instance_method(:clear).source.display
=>
   def clear
     @hash.clear
     self
   end

Returns:

  • (String)

    The method sourcecode as a string

Raises:

  • SourceNotFoundException



114
115
116
# File 'lib/method_source.rb', line 114

def source
  MethodSource.source_helper(source_location, defined?(name) ? name : inspect)
end