secret
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
$ rvm use 1.9.3
Using /home/ngollan/.rvm/gems/ruby-1.9.3-p194
Running /home/ngollan/.rvm/hooks/after_use
 
$ ruby test2.rb
#initialize: private -> private
#i_am_public: public -> public
#i_am_private: private -> private
 
$ rvm use rbx
Using /home/ngollan/.rvm/gems/rbx-head
Running /home/ngollan/.rvm/hooks/after_use
 
$ RBXOPT="-X19" ruby test2.rb
#initialize: private -> public
#i_am_public: public -> public
#i_am_private: private -> private
test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
module Publicize
def self.included(base)
%w(initialize i_am_public i_am_private).each do |m|
STDOUT.write "##{m}: #{base.instance_methods.map(&:to_s).include?(m) ? 'public' : ''}#{base.private_instance_methods.map(&:to_s).include?(m) ? 'private' : ''} -> "
 
base.send :alias_method, "old_#{m}", m
base.send :alias_method, m, "outer_#{m}"
 
puts "#{base.instance_methods.map(&:to_s).include?(m) ? 'public' : ''}#{base.private_instance_methods.map(&:to_s).include?(m) ? 'private' : ''}"
end
end
 
def outer_initialize(*args)
old_initialize(*args)
end
 
def outer_i_am_public
end
 
private
 
def outer_i_am_private
end
end
 
class ThatSimple
 
def i_am_public
end
 
private
 
def i_am_private
end
 
public
 
include Publicize
end
test3.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Should be about minimal.
#
# MRI 1.8.7: true, false
# MRI 1.9.3: true, true
# rbx (both 1.9 and 1.8 mode): true, false
class Hello
private
def initialize
end
 
public
def foo
end
 
p private_instance_methods.map(&:to_s).include?('initialize')
 
alias_method :initialize, :foo
 
p private_instance_methods.map(&:to_s).include?('initialize')
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.