千家信息网

Ruby元编程的代码块怎么写

发表于:2024-11-15 作者:千家信息网编辑
千家信息网最后更新 2024年11月15日,这期内容当中小编将会给大家带来有关Ruby元编程的代码块怎么写,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。闭包2.0.0p247 :133 > def my_
千家信息网最后更新 2024年11月15日Ruby元编程的代码块怎么写

这期内容当中小编将会给大家带来有关Ruby元编程的代码块怎么写,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

闭包

2.0.0p247 :133 >   def my_method2.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 MyClass2.0.0p247 :141?>   v2 =22.0.0p247 :142?>   local_variables2.0.0p247 :143?>   def my_method2.0.0p247 :144?>     v3 =32.0.0p247 :145?>     local_variables2.0.0p247 :146?>     end2.0.0p247 :147?>   local_variables2.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 do2.0.0p247 :157 >     puts "#{my_var} in the class definition"2.0.0p247 :158?>   define_method :my_method do2.0.0p247 :159 >       puts "#{my_var} in the method"2.0.0p247 :160?>     end2.0.0p247 :161?>   endSuccess 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_methodSuccess in the method => nil 2.0.0p247 :163 >

共享作用域

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

obj.instance_eval

2.0.0p247 :193 > class MyClass2.0.0p247 :194?>   def initialize2.0.0p247 :195?>     @v = 12.0.0p247 :196?>     end2.0.0p247 :197?>   end => nil 2.0.0p247 :198 > obj = MyClass.new => # 2.0.0p247 :199 > obj.instance_eval do2.0.0p247 :200 >     self2.0.0p247 :201?>   @v2.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 C2.0.0p247 :207?>   def initialize2.0.0p247 :208?>     @x,@y = 1,22.0.0p247 :209?>     end2.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 CleanRoom2.0.0p247 :216?>   def complex_calculation2.0.0p247 :217?>     p 'complex_calculation'2.0.0p247 :218?>     end2.0.0p247 :219?>   def2.0.0p247 :220 >       do_something2.0.0p247 :221?>     end2.0.0p247 :222?>   end => nil 2.0.0p247 :223 > clean = CleanRoom.new => # 2.0.0p247 :224 > clean.instance_eval do2.0.0p247 :225 >     if complex_calculation > 102.0.0p247 :226?>      do_something2.0.0p247 :227?>     end2.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_proc2.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 * 22.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_dou2.0.0p247 :281?>    p = Proc.new { return 10}2.0.0p247 :282?>    result = p.call2.0.0p247 :283?>    return result * 22.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 >

上述就是小编为大家分享的Ruby元编程的代码块怎么写了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

0