本文簡要介紹ruby語言中 Class.new
的用法。
用法
new(super_class=Object) → a_class
new(super_class=Object) { |mod| ... } → a_class
使用給定的超類創建一個新的匿名(未命名)類(如果沒有給出參數,則為 Object
)。您可以通過將類對象分配給常量來為類命名。
如果給出了一個塊,則將其傳遞給類對象,並在此類的上下文中評估該塊,例如 class_eval
。
fred = Class.new do
def meth1
"hello"
end
def meth2
"bye"
end
end
a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
a.meth1 #=> "hello"
a.meth2 #=> "bye"
如果您想像普通類一樣對待它,請將類分配給一個常量(名稱以大寫開頭)。
相關用法
- Ruby Class.superclass用法及代碼示例
- Ruby Class.inherited用法及代碼示例
- Ruby Class.allocate用法及代碼示例
- Ruby Class.file用法及代碼示例
- Ruby Class.subclasses用法及代碼示例
- Ruby Class類用法及代碼示例
- Ruby Closure類用法及代碼示例
- Ruby ClosedError類用法及代碼示例
- Ruby CStructEntity.[]=用法及代碼示例
- Ruby Context.save_history=用法及代碼示例
- Ruby CSV.header_convert用法及代碼示例
- Ruby Constants模塊用法及代碼示例
- Ruby CMath tanh()用法及代碼示例
- Ruby CSV.skip_lines用法及代碼示例
- Ruby CGI.new用法及代碼示例
- Ruby Comparable.between?用法及代碼示例
- Ruby CGI.print用法及代碼示例
- Ruby CGI.http_header用法及代碼示例
- Ruby CMath cos()用法及代碼示例
- Ruby Complex.arg用法及代碼示例
- Ruby CSV.table用法及代碼示例
- Ruby CSV.force_quotes?用法及代碼示例
- Ruby CParser模塊用法及代碼示例
- Ruby CSV.unconverted_fields?用法及代碼示例
- Ruby ComposedSet類用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Class.new。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。