Created
April 21, 2013 08:39
-
-
Save windwiny/5428930 to your computer and use it in GitHub Desktop.
ruby method method demo
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
class C1 | |
def a;end | |
private | |
def e;end | |
end | |
C1.public_methods false #=> [:allocate, :new, :superclass] | |
c1=C1.new | |
c1.public_methods false #=> [:a] | |
c1.private_methods false #=> [:e] | |
c1.protected_methods false #=> [] | |
c1.methods false #=> [] | |
def c1.x1;end | |
c1.methods.grep /^x/ #=> [:x1] | |
c1.methods false #=> [:x1] | |
c1.singleton_methods #=> [:x1] | |
def c1.zzz;end | |
c1.methods false #=> [:x1, :zzz] | |
c1.singleton_methods ##=> [:x1, :zzz] | |
class C1 | |
def self.s1;end | |
private | |
def self.s2;end | |
end | |
class C1 | |
protected | |
def self.xt ;end | |
end | |
class C1 | |
public | |
def self.y1;end | |
protected | |
def self.y2;end | |
private | |
def self.y3;end | |
end | |
class C1 | |
public | |
def z1;end | |
protected | |
def z2;end | |
private | |
def z3;end | |
end | |
c1.public_methods.grep /^.{1,3}$/ #=> [:x1, :zzz, :a, :z1, :pry, :===, :=~, :!~, :<=>, :dup, :tap, :==, :!, :!=] | |
c1.protected_methods.grep /^.{1,3}$/ #=> [:z2] | |
c1.private_methods.grep /^.{1,3}$/ #=> [:e, :z3, :P, :log, :X1, :`, :p, :gem, :pp] | |
c2=C1.new | |
c2.private_methods.grep /^.{1,3}$/ #=> [:e, :z3, :P, :log, :X1, :x1, :`, :p, :gem, :pp] | |
c2.methods false #=> [] | |
c2.z1 | |
c2.public_methods(false).grep /z1/ #=> [:z1] | |
class C1 | |
public | |
def xk;end | |
end | |
c1.methods false #=> [:x1, :zzz] | |
c1.xk | |
C1.singleton_methods #=> [:s1, :s2, :xt, :y1, :y2, :y3] | |
C1.instance_methods false #=> [:a, :z1, :z2, :xk] | |
C1.public_instance_methods false #=> [:a, :z1, :xk] | |
C1.protected_instance_methods false #=> [:z2] | |
C1.private_instance_methods false #=> [:e, :z3] | |
(c1.public_methods | c1.protected_methods).sort == c1.methods.sort #=> true | |
(C1.public_instance_methods | C1.protected_instance_methods ).sort == C1.instance_methods.sort #=> true | |
# all expect ok. | |
Object.instance_methods.grep(/method/).sort #=> | |
[ | |
:define_singleton_method, | |
:method, | |
:methods, | |
:private_methods, | |
:protected_methods, | |
:public_method, | |
:public_methods, | |
:singleton_methods, | |
] | |
Module.instance_methods.grep(/method/).sort == Object.methods.grep(/method/).sort | |
Object.methods.grep(/method/) #=> | |
[ | |
:private_instance_methods, | |
:instance_methods, | |
:protected_instance_methods, | |
:public_instance_methods, | |
:private_methods, | |
:methods, | |
:protected_methods, | |
:public_methods, | |
:singleton_methods, | |
:define_singleton_method, | |
:instance_method, | |
:method, | |
:public_method, | |
:public_instance_method, | |
:private_class_method, | |
:public_class_method, | |
:method_defined?, | |
:private_method_defined?, | |
:protected_method_defined?, | |
:public_method_defined?, | |
] | |
Kernel.private_instance_methods.grep(/method/).sort #=> | |
[ | |
:__method__, | |
] | |
Object.private_instance_methods.grep(/method/).sort #=> | |
[ | |
:__method__, | |
:method_missing, | |
:singleton_method_added, | |
:singleton_method_removed, | |
:singleton_method_undefined, | |
] | |
Module.private_instance_methods.grep(/method/).sort #=> | |
[ | |
:__method__, | |
:alias_method, | |
:define_method, | |
:method_added, | |
:method_missing, | |
:method_removed, | |
:method_undefined, | |
:remove_method, | |
:singleton_method_added, | |
:singleton_method_removed, | |
:singleton_method_undefined, | |
:undef_method, | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment