MethodSource

Constants

VERSION

Public Class Methods

comment_helper(source_location) click to toggle source

Helper method responsible for opening source file and buffering up the comments for a specified method. Defined here to avoid polluting `Method` class. @param [Array] source_location The array returned by Method#source_location @return [String] The comments up to the point of the method.

# File lib/method_source.rb, line 57
def self.comment_helper(source_location)
  return nil if !source_location.is_a?(Array)

  file_name, line = source_location
  File.open(file_name) do |file|
    buffer = ""
    (line - 1).times do
      line = file.readline
      # Add any line that is a valid ruby comment,
      # but clear as soon as we hit a non comment line.
      if (line =~ /^\s*#/) || (line =~ /^\s*$/)
        buffer << line.lstrip
      else
        buffer.replace("")
      end
    end

    buffer
  end
end
source_helper(source_location) click to toggle source

Helper method responsible for extracting method body. Defined here to avoid polluting `Method` class. @param [Array] source_location The array returned by Method#source_location @return [File] The opened source file

# File lib/method_source.rb, line 35
def self.source_helper(source_location)
  return nil if !source_location.is_a?(Array)

  file_name, line = source_location
  File.open(file_name) do |file|
    (line - 1).times { file.readline }

    code = ""
    loop do
      val = file.readline
      code << val

      return code if valid_expression?(code)
    end
  end
end
valid_expression?(str) click to toggle source

Determine if a string of code is a valid Ruby expression. @param [String] code The code to validate. @return [Boolean] Whether or not the code is a valid Ruby expression. @example

valid_expression?("class Hello") #=> false
valid_expression?("class Hello; end") #=> true
# File lib/method_source.rb, line 16
def self.valid_expression?(str)
  if defined?(Rubinius::Melbourne19) && RUBY_VERSION =~ /^1\.9/
    Rubinius::Melbourne19.parse_string(str)
  elsif defined?(Rubinius::Melbourne)
    Rubinius::Melbourne.parse_string(str)
  else
    catch(:valid) {
      eval("BEGIN{throw :valid}\n#{str}")
    }
  end
  true
rescue SyntaxError
  false
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.