本文簡要介紹ruby語言中 Struct.struct[name] = 的用法。
用法
struct[name] = value → value
struct[n] = value → value
為成員賦值。
給定符號或字符串參數name,將給定的value分配給命名成員;返回 value :
Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe[:zip] = 54321 # => 54321
joe # => #<struct Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=54321>
如果 name 不是成員的名稱,則引發 NameError 。
給定整數參數 n,如果 n 在範圍內,則將給定的 value 分配給 n -th 成員;見Array Indexes:
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe[2] = 54321 # => 54321
joe[-3] = 'Joseph Smith' # => "Joseph Smith"
joe # => #<struct Customer name="Joseph Smith", address="123 Maple, Anytown NC", zip=54321>
如果 n 超出範圍,則引發 IndexError 。
相關用法
- Ruby Struct.struct[name]用法及代碼示例
- Ruby Struct.select用法及代碼示例
- Ruby Struct.self ==用法及代碼示例
- Ruby Struct.size用法及代碼示例
- Ruby Struct.each用法及代碼示例
- Ruby Struct.length用法及代碼示例
- Ruby Struct.filter用法及代碼示例
- Ruby Struct.deconstruct_keys用法及代碼示例
- Ruby Struct.new用法及代碼示例
- Ruby Struct.inspect用法及代碼示例
- Ruby Struct.dig用法及代碼示例
- Ruby Struct.to_a用法及代碼示例
- Ruby Struct.to_h用法及代碼示例
- Ruby Struct.to_s用法及代碼示例
- Ruby Struct.members用法及代碼示例
- Ruby Struct.values用法及代碼示例
- Ruby Struct.deconstruct用法及代碼示例
- Ruby Struct.hash用法及代碼示例
- Ruby Struct.each_pair用法及代碼示例
- Ruby Struct.values_at用法及代碼示例
- Ruby Struct each()用法及代碼示例
- Ruby Struct filter()用法及代碼示例
- Ruby Struct inspect()用法及代碼示例
- Ruby Struct size()用法及代碼示例
- Ruby Struct eql?()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Struct.struct[name] =。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
