Skip to content

Commit

Permalink
experiment #2 : identify the privatazable_methods and their locations
Browse files Browse the repository at this point in the history
  • Loading branch information
alainravet committed Jan 5, 2016
1 parent 86ad5eb commit 6fbecdd
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
4 changes: 2 additions & 2 deletions sandbox/experiment_1.rb
Expand Up @@ -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'
Expand All @@ -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
79 changes: 79 additions & 0 deletions 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
17 changes: 0 additions & 17 deletions sandbox/size_two_stack.rb

This file was deleted.

10 changes: 10 additions & 0 deletions sandbox/fixture_classes.rb → sandbox/utils/fixture_classes.rb
Expand Up @@ -15,8 +15,18 @@ def bar
end

def too
zoo
pzoo
"too"
end
def zoo
"zoo"
end

private
def pzoo
"zoo"
end
end
end

Expand Down
File renamed without changes.

0 comments on commit 6fbecdd

Please sign in to comment.