Ruby meta programming 1(binding/send/freeze/remove_method)

#binding
#like Dependency Injection in java 
10.times{ |i| print("=")}
puts("binding")
class Demo
  def initialize(n)
    @secret = n
  end
  def get_binding
    return binding()
  end
end

k1 = Demo.new(99)
b1 = k1.get_binding
k2 = Demo.new(-3)
b2 = k2.get_binding

#find @secret variable in b1 context
p eval("@secret", b1)   #=> 99
#find @secret variable in b2 context
p eval("@secret", b2)   #=> -3
#find @secret variable in main context
p eval("@secret")       #=> nil

class MyClass
    @@x = " x"
    def initialize(s)
        @mystr = s
    end
    def getBinding
        return binding()
    end
end

class MyOtherClass 
    @@x = " y"
    def initialize(s)
        @mystr = s
    end
    def getBinding
        return binding()
    end
end

@mystr = self.inspect
@@xx = " some other value"

ob1 = MyClass.new("ob1 string")
ob2 = MyClass.new("ob2 string")
ob3 = MyOtherClass.new("ob3 string")

# will try find instance variable @mystr and class variable @@x in ob1's binding
puts(eval("@mystr << @@x", ob1.getBinding))
puts(eval("@mystr << @@x", ob2.getBinding))
puts(eval("@mystr << @@x", ob3.getBinding))
puts(eval("@mystr << @@xx", binding))

# send
# like Reflection in java
10.times{ |i| print("=")}
puts("using send to call method")
name = "Fred" 
puts( name.send( :reverse ) )  #=> derF 
puts( name.send( :upcase ) )  #=> FRED 

10.times{ |i| print("=")}
puts("using send to create method")
class X
    def a
        puts("method a")
    end

    def addMethod( m, &block )
        #define a class method
        self.class.send( :define_method, m , &block )
    end
end

ob = X.new
ob.instance_variable_set("@aname", "Bert")
ob.addMethod( :xyz ) { puts("My name is #{@aname}") } 
ob.xyz

ob.freeze
# freeze will not effect add method
ob.addMethod( :abc ) { puts("abc  My name is #{@aname}") } 
ob.abc

10.times{ |i| print("=")}
puts("remove method")
puts( "hello".reverse ) 
class String 
   remove_method( :reverse ) 
end 
# puts( "hello".reverse )  #NoMethodError

10.times{ |i| print("=")}
puts("undef_method method")
class Y
    def somemethod
        puts("Y's somemethod")
    end
end

class Z < Y
    def somemethod
        puts("Z's somemethod")
    end 
end

zob = Z.new
zob.somemethod

class Z
    undef_method( :somemethod ) 
end

#zob.somemethod #undefined method error

10.times{ |i| print("=")}
puts("freeze object")
s = "Hello" 
s << " world"
p s
s.freeze 
if !(s.frozen?) then
  s << " !!!";
end
#s << " !!!"; # RuntimeError

10.times{ |i| print("=")}
puts("create class dynamicly")
puts("What shall we call this class? ") 
className = gets.strip().capitalize() 
Object.const_set(className,Class.new)   
puts("I'll give it a method called 'myname'" ) 
className = Object.const_get(className) 
className::module_eval{ define_method(:myname){  
   puts("The name of my class is '#{self.class}'" ) }  
} 
x = className.new 
x.myname 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值