这期内容当中小编将会给大家带来有关Ruby元编程的代码块怎么写,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
闭包
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 => #<MyClass:0x007fdaea070450> 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 => #<MyClass:0x007fdaea99f620 @v=1> 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 => #<CleanRoom:0x007fdaea8d5618> 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} => #<Proc:0x007fdaec009a00@(irb):235> 2.0.0p247 :236 > inc.call(2) => 4 2.0.0p247 :237 > 2.0.0p247 :238 > dec = lambda {|x| x-1} => #<Proc:0x007fdaea883728@(irb):238 (lambda)> 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}"} => #<Proc:0x007fdaea048fb8@(irb):254> 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"} => #<Proc:0x007fdaeaadf5f8@(irb):262> 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 } => #<Proc:0x007fdaeaa257c0@(irb):277 (lambda)> 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 } => #<Proc:0x007fdaea9f6da8@(irb):288 (lambda)> 2.0.0p247 :289 > p2 = lambda {|x| x + 1} => #<Proc:0x007fdaea9e5f80@(irb):289 (lambda)> 2.0.0p247 :290 >
重访方法
2.0.0p247 :304 > obj = MyClass.new(1) => #<MyClass:0x007fdaea99d4d8 @x=1> 2.0.0p247 :305 > m = obj.method :my_method => #<Method: MyClass#my_method> 2.0.0p247 :306 > m.call => 1 2.0.0p247 :307 > 2.0.0p247 :308 > 2.0.0p247 :309 > unbound = m.unbind => #<UnboundMethod: MyClass#my_method> 2.0.0p247 :310 > ano = MyClass.new(2) => #<MyClass:0x007fdaea966078 @x=2> 2.0.0p247 :311 > m = unbound.bind(ano) => #<Method: MyClass#my_method> 2.0.0p247 :312 > m.call => 2 2.0.0p247 :313 >
上述就是小编为大家分享的Ruby元编程的代码块怎么写了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。