您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Ruby元編程的代碼塊怎么寫(xiě),文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
閉包
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 >
作用域
塊作用域 作用域門(mén)
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 >
凈結(jié)室
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
可調(diào)用對(duì)象
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 對(duì)比 一般優(yōu)先使用 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元編程的代碼塊怎么寫(xiě)了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。