日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ruby 元编程

發布時間:2025/3/19 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ruby 元编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Ruby 隨著美元在抽象的層面語言的編程支持。相當強大,假定商業模式有一個非常大的階段似性別,我相信元編程讓你節省大量的代碼。

對于一小部分,如以下。(在這里,我們只使用一個class_eval , 并且instance_eval, module_eval, Function.parameters等大量方法能夠調用)

比方 。依據業務需求。須要創建多個有相似方法名的類, 比方方法都命令為 include_xxx, append_xxx, delete_xxx, ?xxxs等 xxx 為詳細的業務類。

那么能夠設計例如以下一個類。 實現此功能。

module Factory
? ? def ?self.build_class(classname, cls_type)
? ? ? ? sub_clss = cls_type.to_s.downcase
? ? ? ? new_cls.class_eval <<-CLASSDEF
? ? ? ? ? ? def initialize
? ? ? ? ? ? ? ? @container = []
? ? ? ? ? ? end

? ? ? ? ? ? def append_#{sub_clss}(key)
? ? ? ? ? ? ? ? @container << key
? ? ? ? ? ? end

? ? ? ? ? ? def delete_#{sub_clss}(key)
? ? ? ? ? ? ? ? @container.delete_if{|e| e == key}
? ? ? ? ? ? end

? ? ? ? ? ? def include_#{sub_clss}?(key)
? ? ? ? ? ? ? ? @container.find {|e| e == key} ? true : false
? ? ? ? ? ? end
? ? ? ? ? ? def traverse

? ? ? ? ? ? ? ? puts "notice container elements as follows"
? ? ? ? ? ? ? ? @container.each {|e| puts e}
? ? ? ? ? ? end
? ? ? ? CLASSDEF
? ? ? ? new_cls
? ? end
end

執行結果例如以下:

c1 = (Factory.build_class "C1", String)
n = c1.new
n.append_string("abc")?
puts n.traverse
puts ">>>>", n.include_string?

("abc")
puts ">>>.",n.include_string?("123")
n.delete_string("abc")
puts n.traverse


版權聲明:本文博客原創文章,博客,未經同意,不得轉載。

總結

以上是生活随笔為你收集整理的Ruby 元编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。