Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code block of Ruby metaprogramming

Shulou Source: shulou.com Published: 2022-06-03 05:57:04 09月16日 Update

In this issue, the editor will bring you how to write the code blocks of Ruby metaprogramming. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Closure

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 >

Scope

Block scope gate

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 >

Flattening scope

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 >

Shared scope

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 2.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 >

Clean junction room

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

Callable object

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

& operator

2.0.0p247: 242 > def math 2.0.0p247: 2443? > yield (AMagneb) 2.0.0p247: 244? > end = > nil 2.0.0p247: 245 > def teach_math 2.0.0p247: 246? > puts "Let's do the math:" 2.0.0p247: 247? > puts math (apenb) & operation) 2.0.0p247: 24824? > end = > nil 2.0.0p247: 249 > teach_math (3Magazine 4) {| XMague 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 and lambda generally give priority to using 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 > the same effect in Ruby 1.9 2.0.0p247: 288 > p =-> {x + 1} = > # 2.0.0p247: 289 > p2 = lambda {| x | x + 1} = > # 2.0.0p247: 290 >

Revisit method

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 > this is how the code block of Ruby metaprogramming shared by the editor is written. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Function code programming content analysis specialty small and medium rich content object that is flat operator effect article method more knowledge article industry perspective Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Microsoft Shulou Technology NVidia MySQL