Ruby Cheat Sheet#1 反射

01 Nov 2010 / Edit / History


irb(main):001:0> a = Object.new
=> #<Object:0x7ff7a6ff6388>
irb(main):002:0> a.object_id #获取对象ID
=> 70350817702340
irb(main):003:0> a.class #获取对象类型
=> Object
irb(main):004:0> Integer.superclass #获取父类
=> Numeric
irb(main):006:0> Numeric.superclass #...
=> Object
irb(main):007:0> Object.superclass #Object父类为 nil
=> nil
irb(main):011:0> Integer.ancestors #获取类的父类及 include 的类
=> [Integer, Precision, Numeric, Comparable, Object, Kernel]
irb(main):013:0> Integer.class #类的类型是 Class
=> Class
irb(main):019:0> 1.methods #查找对象的方法
=> ["%", ...]
irb(main):020:0> 1.public_methods #查找对象的公开方法
irb(main):022:0> 1.protected_methods #查找对象的保护方法
irb(main):023:0> 1.private_methods #查找对象的私有方法

irb(main):029:0> class << a
irb(main):030:1> def foo; end
irb(main):031:1> end
=> nil
irb(main):032:0> a.singleton_methods #查找对象的单例方法
=> ["foo"]

irb(main):033:0> a.methods.grep /methods/ #快速查询方法
=> ["public_methods", "methods", "singleton_methods", "protected_methods", "private_methods"]

irb(main):034:0> Integer.methods #类也是对象,也有 :methods
=> ["private_class_method", ...]
irb(main):036:0> Integer.instance_methods #获取实例方法
irb(main):037:0> Integer.public_instance_methods #...
irb(main):038:0> Integer.protected_instance_methods #...
irb(main):039:0> Integer.private_instance_methods #...

irb(main):041:0> 1.method :times #获取方法对象
=> #<Method: Fixnum(Integer)#times>
irb(main):042:0> 1.method(:times).call #执行方法对象
=> #<Enumerable::Enumerator:0x7ff7a703fb78>
irb(main):044:0> 1.send :times #执行方法对象
=> #<Enumerable::Enumerator:0x7ff7a7019090>
irb(main):045:0> 1.__send__ :times #执行方法对象
=> #<Enumerable::Enumerator:0x7ff7a6ffd1b0>

irb(main):047:0> m = 1.method :times
=> #<Method: Fixnum(Integer)#times>
irb(main):048:0> m.name #方法名称
=> "times"
irb(main):051:0> m.owner #方法所在的类
=> Integer
irb(main):054:0> m.to_proc #转成 Proc 对象
=> #<Proc:0x00007ff7a6f955d8@(irb):54>
irb(main):055:0> m.unbind #解除绑定
=> #<UnboundMethod: Fixnum(Integer)#times>
irb(main):056:0> m.arity #最少参数个数
=> 0
irb(main):057:0> m.receiver #绑定的对象
=> 1

irb(main):058:0> m = Integer.instance_method :times #获取未绑定的对象
=> #<UnboundMethod: Integer#times>
irb(main):060:0> m.bind(1) #绑定
=> #<Method: Fixnum(Integer)#times>
irb(main):061:0> m.owner #方法所在的类
=> Integer
irb(main):062:0> m.arity #最少参数个数
=> 0
irb(main):063:0> m.bind(Object.new) #绑定时会做类型检查
TypeError: bind argument must be an instance of Integer
from (irb):63:in `bind'
from (irb):63
from :0
irb(main):075:0> Integer.method_defined? :times #查询类的实例方法是否被定义
=> true
irb(main):076:0> Integer.public_method_defined? :times
=> true
irb(main):077:0> Integer.protected_method_defined? :times
=> false
irb(main):078:0> Integer.private_method_defined? :times
=> false

irb(main):089:0> 1.respond_to? :times #检查对象是否有方法
=> true
irb(main):092:0> 1.respond_to? :raise
=> false
irb(main):093:0> 1.respond_to? :raise, true #同时检查私有方法
=> true
irb(main):106:0> [1.class, 1.class.superclass]
=> [Fixnum, Integer]
irb(main):098:0> 1.is_a? Fixnum #检查1是否是 Fixnum 或其子类的对象
=> true
irb(main):099:0> 1.is_a? Integer #检查1是否是 Integer 或其子类的对象
=> true
irb(main):103:0> 1.instance_of? Fixnum #检查1是否是 Fixnum 的对象
=> true
irb(main):104:0> 1.instance_of? Integer #检查1是否是 Integer 的对象
=> false
irb(main):108:0> Fixnum.instance_methods - Integer.instance_methods #查看 Fixnum 类新添加的实例方法
=> ["%", "<<", "&", ">>", "to_sym", "*", "+", "-", "/", "|", "size", "~", "^", "**", "to_f", "id2name", "[]"]

补1: 刚才忘了变量部分了

irb(main):001:0> class A
irb(main):002:1> @@b = 1
irb(main):003:1> @c = 2
irb(main):004:1> def initialize
irb(main):005:2> @d = 3
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> A.class_variables
=> ["@@b"]
irb(main):010:0> A.class_variable_defined? "@@b"
=> true
irb(main):011:0> A.instance_variables
=> ["@c"]
irb(main):012:0> A.instance_variable_defined? "@c"
=> true
irb(main):014:0> A.instance_variable_set "@c", 15
=> 15
irb(main):015:0> A.instance_variable_get "@c"
=> 15
irb(main):016:0> a = A.new
=> #<A:0x7f55a5717c88 @d=3>
irb(main):018:0> a.instance_variables
=> ["@d"]
irb(main):019:0> a.instance_variable_defined? "@d"
=> true
irb(main):020:0> a.instance_variable_set "@d", 16
=> 16
irb(main):021:0> a.instance_variable_get "@d"
=> 16

Tags: ruby

Related Posts:

comments powered by Disqus
Copyright © 2017 LI Daobing, Powered by github:pages, Jekyll, bootstrap, Designed by quanquan
Fork me on GitHub