Module: TLDR::RubyUtil

Defined in:
lib/tldr/ruby_util.rb

Class Method Summary collapse

Class Method Details

.find_prism_def_node_for(method) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tldr/ruby_util.rb', line 11

def self.find_prism_def_node_for(method)
  require "prism"

  iseq = RubyVM::InstructionSequence.of(method).to_a
   = iseq[4]
  method_name = iseq[5]

  file_path, line_number = method.source_location
  parse_prism_ast(file_path).breadth_first_search { |node|
    node.type == :def_node &&
      line_number == node.start_line &&
      method_name == node.name.to_s &&
      [:code_location] == [node.start_line, node.start_column, node.end_line, node.end_column]
  }
end

.parse_prism_ast(file_path) ⇒ Object



27
28
29
30
# File 'lib/tldr/ruby_util.rb', line 27

def self.parse_prism_ast(file_path)
  @prism_ast = Thread.current[:prism_parse_results] ||= {}
  @prism_ast[file_path] ||= Prism.parse(File.read(file_path)).value
end

.parsing_with_prism?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/tldr/ruby_util.rb', line 7

def self.parsing_with_prism?
  @parsing_with_prism ||= RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
end

.versionObject



3
4
5
# File 'lib/tldr/ruby_util.rb', line 3

def self.version
  @version ||= Gem::Version.new(RUBY_VERSION)
end