Ruby元编程的代码块怎么写
创始人
2024-12-26 04:43:36
0次

闭包

2.0.0p247 :133 >   def my_method
2.0.0p247 :134?>     x="GoodBye"
2.0.0p247 :135?>     yield("cruel")
2.0.0p247 :136?>   end
 => nil 
2.0.0p247 :137 > x = "Hello"
 => "Hello" 
2.0.0p247 :138 > my_method {|y| "#{x}, #{y} world"}
 => "Hello, cruel world" 
2.0.0p247 :139 >

作用域

块作用域   作用域门

2.0.0p247 :139 > v1 =1
 => 1 
2.0.0p247 :140 > class MyClass
2.0.0p247 :141?>   v2 =2
2.0.0p247 :142?>   local_variables
2.0.0p247 :143?>   def my_method
2.0.0p247 :144?>     v3 =3
2.0.0p247 :145?>     local_variables
2.0.0p247 :146?>     end
2.0.0p247 :147?>   local_variables
2.0.0p247 :148?>   end
 => [:v2] 
2.0.0p247 :149 > obj = MyClass.new
 => # 
2.0.0p247 :150 > obj.my_method
 => [:v3] 
2.0.0p247 :151 > obj.my_method
 => [:v3] 
2.0.0p247 :152 > local_variables
 => [:v1, :x, :p, :obj, :a, :_] 
2.0.0p247 :153 > 	

扁平化作用域

2.0.0p247 :156 > MyClass = Class.new do
2.0.0p247 :157 >     puts "#{my_var} in the class definition"
2.0.0p247 :158?>   define_method :my_method do
2.0.0p247 :159 >       puts "#{my_var} in the method"
2.0.0p247 :160?>     end
2.0.0p247 :161?>   end
Success in the class definition
(irb):156: warning: already initialized constant MyClass
(irb):9: warning: previous definition of MyClass was here
 => MyClass 
2.0.0p247 :162 > MyClass.new.my_method
Success in the method
 => nil 
2.0.0p247 :163 >

共享作用域

2.0.0p247 :178 >   def define_methods
2.0.0p247 :179?>   shared = 0 
2.0.0p247 :180?>   Kernel.send :define_method, :course do
2.0.0p247 :181 >       shared
2.0.0p247 :182?>     end
2.0.0p247 :183?>   Kernel.send :define_method, :inc do |x|
2.0.0p247 :184 >       shared += x
2.0.0p247 :185?>     end
2.0.0p247 :186?>   end

obj.instance_eval

2.0.0p247 :193 > class MyClass
2.0.0p247 :194?>   def initialize
2.0.0p247 :195?>     @v = 1
2.0.0p247 :196?>     end
2.0.0p247 :197?>   end
 => nil 
2.0.0p247 :198 > obj = MyClass.new
 => # 
2.0.0p247 :199 > obj.instance_eval do
2.0.0p247 :200 >     self
2.0.0p247 :201?>   @v
2.0.0p247 :202?>   end
 => 1 
2.0.0p247 :203 > v = 2
 => 2 
2.0.0p247 :204 > obj.instance_eval {@v = v}
 => 2 
2.0.0p247 :205 > obj.instance_eval {@v}
 => 2 
2.0.0p247 :206 >

C.new.instance_exec(3)

2.0.0p247 :206 > class C
2.0.0p247 :207?>   def initialize
2.0.0p247 :208?>     @x,@y = 1,2
2.0.0p247 :209?>     end
2.0.0p247 :210?>   end
 => nil 
2.0.0p247 :211 > C.new.instance_exec(3) {|arg| (@x + @y) * arg}
 => 9 
2.0.0p247 :212 >

净结室

2.0.0p247 :215 > class CleanRoom
2.0.0p247 :216?>   def complex_calculation
2.0.0p247 :217?>     p 'complex_calculation'
2.0.0p247 :218?>     end
2.0.0p247 :219?>   def
2.0.0p247 :220 >       do_something
2.0.0p247 :221?>     end
2.0.0p247 :222?>   end
 => nil 
2.0.0p247 :223 > clean = CleanRoom.new
 => # 
2.0.0p247 :224 > clean.instance_eval do
2.0.0p247 :225 >     if complex_calculation > 10
2.0.0p247 :226?>      do_something
2.0.0p247 :227?>     end
2.0.0p247 :228?>   end

可调用对象

2.0.0p247 :235 > inc = Proc.new {|x|  x*2}
 => # 
2.0.0p247 :236 > inc.call(2)
 => 4 
2.0.0p247 :237 > 
2.0.0p247 :238 >   dec = lambda {|x| x-1}
 => # 
2.0.0p247 :239 > dec.class
 => Proc 
2.0.0p247 :240 > dec.call(2)
 => 1 
2.0.0p247 :241 >

&操作符

2.0.0p247 :242 >   def math(a,b)
2.0.0p247 :243?>    yield(a,b)
2.0.0p247 :244?>   end
 => nil 
2.0.0p247 :245 >  def teach_math(a,b,&operation)
2.0.0p247 :246?>   puts "Let's do the math:"
2.0.0p247 :247?>   puts math(a,b,&operation)
2.0.0p247 :248?>   end
 => nil 
2.0.0p247 :249 > teach_math(3,4){|x,y| x * y}
Let's do the math:
12
 => nil 
2.0.0p247 :250 >
2.0.0p247 :251 >   def my_method(&the_proc)
2.0.0p247 :252?>      the_proc
2.0.0p247 :253?>   end
 => nil 
2.0.0p247 :254 > p = my_method{ |name| "Hello, #{name}"}
 => # 
2.0.0p247 :255 > p.class
 => Proc 
2.0.0p247 :256 > p.call("BIll")
 => "Hello, BIll" 
2.0.0p247 :257 >
2.0.0p247 :262 > my_proc = proc{"Bill"}
 => # 
2.0.0p247 :263 > my_method("hello",&my_proc)
hello the , Bill
 => nil 
2.0.0p247 :264 >

proc 和 lambda 对比  一般优先使用 lambda

2.0.0p247 :274 >   def double(obj)
2.0.0p247 :275?>     obj.call * 2
2.0.0p247 :276?>   end
 => nil 
2.0.0p247 :277 >  l = lambda { return 10 }
 => # 
2.0.0p247 :278 >  double(l)
 => 20 
2.0.0p247 :279 > 
2.0.0p247 :280 >   def another_dou
2.0.0p247 :281?>    p = Proc.new { return 10}
2.0.0p247 :282?>    result = p.call
2.0.0p247 :283?>    return result * 2
2.0.0p247 :284?>   end
 => nil 
2.0.0p247 :285 >   another_dou
 => 10 
2.0.0p247 :286 >
同样效果在 Ruby 1.9 中之后
2.0.0p247 :288 >   p = -> { x + 1 }
 => # 
2.0.0p247 :289 >  p2 = lambda {|x| x + 1}
 => # 
2.0.0p247 :290 >

重访方法

2.0.0p247 :304 >  obj = MyClass.new(1)
 => # 
2.0.0p247 :305 >  m = obj.method :my_method
 => # 
2.0.0p247 :306 >  m.call
 => 1 
2.0.0p247 :307 > 
2.0.0p247 :308 >   
2.0.0p247 :309 >   unbound = m.unbind
 => # 
2.0.0p247 :310 >   ano = MyClass.new(2)
 => # 
2.0.0p247 :311 >  m = unbound.bind(ano)
 => # 
2.0.0p247 :312 > m.call
 => 2 
2.0.0p247 :313 >

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...