Class: IRB::Command::Ls
Defined Under Namespace
Classes: EvaluationError
Instance Attribute Summary
Attributes inherited from Base
#irb_context
Instance Method Summary
collapse
Methods inherited from Base
category, description, doc_dialog_content, execute, help_message, #initialize
Instance Method Details
#class_method_map(classes, dumped_mods) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/irb/command/ls.rb', line 86
def class_method_map(classes, dumped_mods)
dumped_methods = Array.new
classes.map do |mod|
next if dumped_mods.include? mod
dumped_mods << mod
methods = mod.public_instance_methods(false).select do |method|
if dumped_methods.include? method
false
else
dumped_methods << method
true
end
end
[mod, methods]
end.compact
end
|
#dump_methods(o, klass, obj) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/irb/command/ls.rb', line 71
def dump_methods(o, klass, obj)
singleton_class = begin Kernel.instance_method(:singleton_class).bind(obj).call; rescue TypeError; nil end
dumped_mods = Array.new
ancestors = klass.ancestors
ancestors = ancestors.reject { |c| c >= Object } if klass < Object
singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| c >= Class }
maps = class_method_map(singleton_ancestors, dumped_mods) + class_method_map(ancestors, dumped_mods)
maps.each do |mod, methods|
name = mod == singleton_class ? "#{klass}.methods" : "#{mod}#methods"
o.dump(name, methods)
end
end
|
#evaluate(code) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/irb/command/ls.rb', line 25
def evaluate(code)
@irb_context.workspace.binding.eval(code)
rescue Exception => e
puts "#{e.class}: #{e.message}"
raise EvaluationError
end
|
#execute(arg) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/irb/command/ls.rb', line 32
def execute(arg)
if match = arg.match(/\A(?<target>.+\s|)(-g|-G)\s+(?<grep>.+)$/)
target = match[:target]
grep = Regexp.new(match[:grep])
elsif match = arg.match(/\A((?<target>.+),|)\s*grep:(?<grep>.+)/)
target = match[:target] || ''
grep_regexp_code = match[:grep]
else
target = arg.strip
end
if target.empty?
obj = irb_context.workspace.main
locals = irb_context.workspace.binding.local_variables
else
obj = evaluate(target)
end
if grep_regexp_code
grep = evaluate(grep_regexp_code)
end
o = Output.new(grep: grep)
klass = Kernel.instance_method(:class).bind(obj).call
obj_is_class_or_module = Module === obj
klass = obj_is_class_or_module ? obj : klass
o.dump("constants", obj.constants) if obj_is_class_or_module
dump_methods(o, klass, obj)
o.dump("instance variables", Kernel.instance_method(:instance_variables).bind(obj).call)
o.dump("class variables", klass.class_variables)
o.dump("locals", locals) if locals
o.print_result
rescue EvaluationError
end
|