本文簡要介紹ruby語言中 Struct.new 的用法。
用法
new(*member_names, keyword_init: false){|Struct_subclass| ... } → Struct_subclass
new(class_name, *member_names, keyword_init: false){|Struct_subclass| ... } → Struct_subclass
new(*member_names) → Struct_subclass_instance
new(**member_names) → Struct_subclass_instance
Struct.new 返回 Struct 的新子類。新的子類:
-
可能是匿名的,或者可能具有
class_name給出的名稱。 -
可能有
member_names給出的成員。 -
可能通過普通參數(默認)或關鍵字參數(如果給出
keyword_init: true)進行初始化。
新的子類有自己的方法 ::new ;因此:
Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo
f = Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1>
類名稱
使用字符串參數 class_name ,返回一個名為 Struct 的新子類 Struct::class_name :
Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo
Foo.name # => "Struct::Foo"
Foo.superclass # => Struct
如果沒有字符串參數 class_name ,則返回 Struct 的新匿名子類:
Struct.new(:foo, :bar).name # => nil
堵塞
在給定塊的情況下,創建的子類將屈服於該塊:
Customer = Struct.new('Customer', :name, :address) do |new_class|
p "The new subclass is #{new_class}"
def greeting
"Hello #{name} at #{address}"
end
end # => Struct::Customer
dave = Customer.new('Dave', '123 Main')
dave # => #<struct Struct::Customer name="Dave", address="123 Main">
dave.greeting # => "Hello Dave at 123 Main"
輸出,來自 Struct.new :
"The new subclass is Struct::Customer"
會員姓名
符號參數 member_names 確定新子類的成員:
Struct.new(:foo, :bar).members # => [:foo, :bar]
Struct.new('Foo', :foo, :bar).members # => [:foo, :bar]
新的子類具有對應於 member_names 的實例方法:
Foo = Struct.new('Foo', :foo, :bar)
Foo.instance_methods(false) # => [:foo, :bar, :foo=, :bar=]
f = Foo.new # => #<struct Struct::Foo foo=nil, bar=nil>
f.foo # => nil
f.foo = 0 # => 0
f.bar # => nil
f.bar = 1 # => 1
f # => #<struct Struct::Foo foo=0, bar=1>
單例方法
Struct.new 返回的子類具有以下單例方法:
-
方法
::new創建子類的一個實例:Foo.new # => #<struct Struct::Foo foo=nil, bar=nil> Foo.new(0) # => #<struct Struct::Foo foo=0, bar=nil> Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1> Foo.new(0, 1, 2) # Raises ArgumentError: struct size differs方法
::[]是方法::new的別名。 -
方法
:inspect返回子類的字符串表示:Foo.inspect # => "Struct::Foo" -
方法
::members返回成員名稱的數組:Foo.members # => [:foo, :bar]
關鍵字參數
默認情況下,初始化新子類實例的參數是普通參數(不是關鍵字參數)。使用可選的關鍵字參數 keyword_init: true ,新的子類使用關鍵字參數進行初始化:
# Without keyword_init: true.
Foo = Struct.new('Foo', :foo, :bar)
Foo # => Struct::Foo
Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1>
# With keyword_init: true.
Bar = Struct.new(:foo, :bar, keyword_init: true)
Bar # => # => Bar(keyword_init: true)
Bar.new(bar: 1, foo: 0) # => #<struct Bar foo=0, bar=1>
相關用法
- Ruby Struct.each用法及代碼示例
- Ruby Struct.length用法及代碼示例
- Ruby Struct.filter用法及代碼示例
- Ruby Struct.deconstruct_keys用法及代碼示例
- Ruby Struct.select用法及代碼示例
- Ruby Struct.struct[name]用法及代碼示例
- Ruby Struct.self ==用法及代碼示例
- Ruby Struct.inspect用法及代碼示例
- Ruby Struct.dig用法及代碼示例
- Ruby Struct.to_a用法及代碼示例
- Ruby Struct.to_h用法及代碼示例
- Ruby Struct.to_s用法及代碼示例
- Ruby Struct.size用法及代碼示例
- Ruby Struct.members用法及代碼示例
- Ruby Struct.values用法及代碼示例
- Ruby Struct.deconstruct用法及代碼示例
- Ruby Struct.hash用法及代碼示例
- Ruby Struct.each_pair用法及代碼示例
- Ruby Struct.values_at用法及代碼示例
- Ruby Struct.struct[name] =用法及代碼示例
- Ruby Struct each()用法及代碼示例
- Ruby Struct filter()用法及代碼示例
- Ruby Struct inspect()用法及代碼示例
- Ruby Struct size()用法及代碼示例
- Ruby Struct eql?()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Struct.new。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
