From 6fbecddd7cd248d78df14e8d2024871db1470bbf Mon Sep 17 00:00:00 2001 From: Alain Ravet Date: Tue, 5 Jan 2016 01:23:12 +0100 Subject: [PATCH] experiment #2 : identify the privatazable_methods and their locations --- sandbox/experiment_1.rb | 4 +- sandbox/experiment_2.rb | 79 +++++++++++++++++++++++++ sandbox/size_two_stack.rb | 17 ------ sandbox/{ => utils}/fixture_classes.rb | 10 ++++ sandbox/{ => utils}/size_three_stack.rb | 0 5 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 sandbox/experiment_2.rb delete mode 100644 sandbox/size_two_stack.rb rename sandbox/{ => utils}/fixture_classes.rb (88%) rename sandbox/{ => utils}/size_three_stack.rb (100%) diff --git a/sandbox/experiment_1.rb b/sandbox/experiment_1.rb index 5770aa2..c34c0c5 100644 --- a/sandbox/experiment_1.rb +++ b/sandbox/experiment_1.rb @@ -6,7 +6,7 @@ def private_call?(c1, c2, c3) (c3.event != :return || c1.object_id==c3.object_id) end -require_relative 'size_three_stack' +require_relative 'utils/size_three_stack' $stack = SizeThreeStack.new require 'awesome_print' @@ -25,6 +25,6 @@ def private_call?(c1, c2, c3) tracer.enable do - require_relative 'fixture_classes' + require_relative 'utils/fixture_classes' M::Foo.new.foo end diff --git a/sandbox/experiment_2.rb b/sandbox/experiment_2.rb new file mode 100644 index 0000000..8c4f471 --- /dev/null +++ b/sandbox/experiment_2.rb @@ -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 diff --git a/sandbox/size_two_stack.rb b/sandbox/size_two_stack.rb deleted file mode 100644 index fcfff14..0000000 --- a/sandbox/size_two_stack.rb +++ /dev/null @@ -1,17 +0,0 @@ -class SizeTwoStack - attr_reader :cell_1, :cell_2 - - def push(value) - @cell_2 = @cell_1 - @cell_1 = value - end - - def to_s - [@cell_1, @cell_2].to_s - end -end -__END__ -$stack = TwoElementStack.new -puts [$stack.cell_2, $stack.cell_1].inspect -$stack.push 1 ; $stack.push 2 ; $stack.cell_1 ; $stack.cell_2 -puts [$stack.cell_2, $stack.cell_1].inspect diff --git a/sandbox/fixture_classes.rb b/sandbox/utils/fixture_classes.rb similarity index 88% rename from sandbox/fixture_classes.rb rename to sandbox/utils/fixture_classes.rb index a8a44de..5a19406 100644 --- a/sandbox/fixture_classes.rb +++ b/sandbox/utils/fixture_classes.rb @@ -15,8 +15,18 @@ def bar end def too + zoo + pzoo "too" end + def zoo + "zoo" + end + + private + def pzoo + "zoo" + end end end diff --git a/sandbox/size_three_stack.rb b/sandbox/utils/size_three_stack.rb similarity index 100% rename from sandbox/size_three_stack.rb rename to sandbox/utils/size_three_stack.rb