Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the code written by Ruby metaprogramming?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report