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

Instance Method Summary collapse

Instance Method Details

#commentString

Return the comments associated with the method as a string. (This functionality is only supported in Ruby 1.9 and above)

Examples:

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

Returns:

  • (String)

    The method’s comments as a string



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/method_source.rb', line 106

def comment
  if respond_to?(:source_location)
    comment = MethodSource.comment_helper(source_location)
    
    raise "Cannot locate source for this method: #{name}" if !comment
  else
    raise "Method#comment not supported by this Ruby version (#{RUBY_VERSION})"
  end

  comment
end

#sourceString

Return the sourcecode for the method as a string (This functionality is only supported in Ruby 1.9 and above)

Examples:

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

Returns:

  • (String)

    The method sourcecode as a string



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/method_source.rb', line 87

def source
  if respond_to?(:source_location)
    source = MethodSource.source_helper(source_location)
    
    raise "Cannot locate source for this method: #{name}" if !source
  else
    raise "Method#source not supported by this Ruby version (#{RUBY_VERSION})"
  end
  
  source
end