Class: IRB::ExtendCommand::ShowSource
- Inherits:
-
Nop
show all
- Defined in:
- lib/irb/cmd/show_source.rb
Instance Attribute Summary
Attributes inherited from Nop
#irb_context
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Nop
category, description, execute, #initialize, #irb
Class Method Details
.find_source(str, irb_context) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/irb/cmd/show_source.rb', line 25
def find_source(str, irb_context)
case str
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ eval(str, irb_context.workspace.binding) base = irb_context.workspace.binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object }
file, line = base.const_source_location(str) if base.respond_to?(:const_source_location) when /\A(?<owner>[A-Z]\w*(::[A-Z]\w*)*)#(?<method>[^ :.]+)\z/ owner = eval(Regexp.last_match[:owner], irb_context.workspace.binding)
method = Regexp.last_match[:method]
if owner.respond_to?(:instance_method) && owner.instance_methods.include?(method.to_sym)
file, line = owner.instance_method(method).source_location
end
when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ receiver = eval(Regexp.last_match[:receiver] || 'self', irb_context.workspace.binding)
method = Regexp.last_match[:method]
file, line = receiver.method(method).source_location if receiver.respond_to?(method)
end
if file && line
Source.new(file: file, first_line: line, last_line: find_end(file, line))
end
end
|
16
17
18
19
20
21
22
23
|
# File 'lib/irb/cmd/show_source.rb', line 16
def transform_args(args)
if args.empty? || string_literal?(args)
args
else args.strip.dump
end
end
|
Instance Method Details
#execute(str = nil) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/irb/cmd/show_source.rb', line 70
def execute(str = nil)
unless str.is_a?(String)
puts "Error: Expected a string but got #{str.inspect}"
return
end
source = self.class.find_source(str, @irb_context)
if source && File.exist?(source.file)
show_source(source)
else
puts "Error: Couldn't locate a definition for #{str}"
end
nil
end
|