Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experiment #2 : identify the privatazable_methods and their locations
- Loading branch information
1 parent
86ad5eb
commit 6fbecdd
Showing
5 changed files
with
91 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
TracePointData = Struct.new(:event, :object_id, :lineno, :defined_class, :method_id) | ||
|
||
def private_call?(c1, c2, c3) | ||
(c1.event == :call && c2.event == :line) && | ||
c1.object_id==c2.object_id && | ||
(c3.event != :return || c1.object_id==c3.object_id) | ||
end | ||
|
||
require_relative 'utils/size_three_stack' | ||
$stack = SizeThreeStack.new | ||
require 'set' | ||
$publicly_called_methods = Set.new | ||
$privately_called_methods = Set.new | ||
|
||
require 'awesome_print' | ||
tracer = TracePoint.new do |tp| | ||
$stack.push TracePointData.new(tp.event, tp.self.object_id, tp.lineno, tp.defined_class, tp.method_id) | ||
if tp.event == :call | ||
c1, c2, c3 = $stack.cell_1, $stack.cell_2, $stack.cell_3 | ||
|
||
key = "#{c1.defined_class}##{c1.method_id}" | ||
if private_call?(c1, c2, c3) | ||
$privately_called_methods.add key | ||
else | ||
$publicly_called_methods.add key | ||
end | ||
end | ||
end | ||
|
||
# ------------------------------------------------------------------ | ||
|
||
tracer.enable do | ||
require_relative 'utils/fixture_classes' | ||
M::Foo.new.foo | ||
end | ||
|
||
# ------------------------------------------------------------------ | ||
|
||
only_privately_called_methods = $privately_called_methods - $publicly_called_methods | ||
privatazable_methods = Hash.new{|h, k| h[k] = {} } | ||
|
||
only_privately_called_methods.each{ |key| | ||
klass, method = key.split('#') | ||
klass = Object.const_get(klass) | ||
method = method.to_sym | ||
|
||
if klass.private_instance_methods.include?(method) | ||
privatazable_methods[klass][method] = klass.new.method(method).source_location | ||
end | ||
} | ||
|
||
# ------------------------------------------------------------------ | ||
|
||
puts 'Methods that are only called privately' | ||
ap only_privately_called_methods | ||
|
||
puts 'Methods that can be made private' | ||
ap privatazable_methods | ||
|
||
__END__ | ||
|
||
/Users/alain/.rvm/rubies/ruby-2.2.3/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/alain/dev/private_please/sandbox/experiment_2.rb | ||
|
||
Methods that are only called privately | ||
[ | ||
[0] "M::Foo#zoo", | ||
[1] "M::Foo#pzoo" | ||
] | ||
Methods that can be made private | ||
{ | ||
M::Foo < Object => { | ||
:pzoo => [ | ||
[0] "/Users/alain/dev/private_please/sandbox/utils/fixture_classes.rb", | ||
[1] 27 | ||
] | ||
} | ||
} | ||
|
||
Process finished with exit code 0 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,18 @@ def bar | |
end | ||
|
||
def too | ||
zoo | ||
pzoo | ||
"too" | ||
end | ||
def zoo | ||
"zoo" | ||
end | ||
|
||
private | ||
def pzoo | ||
"zoo" | ||
end | ||
end | ||
end | ||
|
||
|
File renamed without changes.