Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the code written by Ruby metaprogramming?

Shulou Source: shulou.com Published: 2022-06-03 05:57:03 11月17日 Update

This article to share with you is about Ruby metaprogramming code is what, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some gains, not much to say, follow Xiaobian to see it.

1.Kernel#eval

2.0.0p247 :428 > array = ['10','20'] => ["10", "20"] 2.0.0p247 :429 > element = '30' => "30" 2.0.0p247 :430 > eval('array ["a", "b", "c\n"] 2.0.0p247 :424 > x = 'abc' => "abc" 2.0.0p247 :425 > array.instance_eval "self[1] = x" => "abc" 2.0.0p247 :426 > array => ["a", "abc", "c\n"] 2.0.0p247 :427 >2.0.0p247 :450 > def explore_array(method,*arg)2.0.0p247 :451?> ['a','b','c'].send(method, *arg)2.0.0p247 :452?> end => nil 2.0.0p247 :453 > explore_array(:size) => 3 2.0.0p247 :454 >

2. hook function inherited (more such as module included,method_added,method_removed,method_undefined)

2.0.0p247 :463 > class String2.0.0p247 :464?> def self.inherited(subclass)2.0.0p247 :465?> puts "hook function"2.0.0p247 :466?> end2.0.0p247 :467?> end => nil 2.0.0p247 :468 > class MyString

< String;end钩子函数 =>

nil 2.0.0p247 :469 > The above is what Ruby metaprogramming code is, Xiaobian believes that some knowledge points may be what we see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Code more programming functions knowledge articles hooks utility that is work sessions articles methods look knowledge points industry details information information channels follow part Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia Redmi Apple MySQL