類別 Struct

類別 Struct 提供一個方便的方法來建立一個簡單的類別,可以用來儲存和擷取值。

這個範例建立一個 Struct 的子類別,Struct::Customer;第一個參數是一個字串,是子類別的名稱;其他參數是符號,用來決定新子類別的「成員」。

Customer = Struct.new('Customer', :name, :address, :zip)
Customer.name       # => "Struct::Customer"
Customer.class      # => Class
Customer.superclass # => Struct

每個成員都有兩個對應的方法,一個寫入器和一個讀取器,用來儲存和擷取值

methods = Customer.instance_methods false
methods # => [:zip, :address=, :zip=, :address, :name, :name=]

可以使用 ::new 方法建立子類別的實例,並指定成員的值

joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe # => #<struct Struct::Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=12345>

成員值可以這樣管理

joe.name    # => "Joe Smith"
joe.name = 'Joseph Smith'
joe.name    # => "Joseph Smith"

也可以這樣;請注意,成員名稱可以用字串或符號表示

joe[:name]  # => "Joseph Smith"
joe[:name] = 'Joseph Smith, Jr.'
joe['name'] # => "Joseph Smith, Jr."

請參閱 Struct::new

此處內容

首先,其他地方的內容。類別 Struct

另請參閱 Data,這是一個類似但更嚴格的概念,用於定義不可變的值物件。

這裡,類別 Struct 提供的方法對於下列用途很有用

Struct 子類別的建立方法

查詢方法

用於比較的函式

用於擷取的函式

用於指定的方法

用於反覆運算的方法

用於轉換的方法

公開類別方法

json_create(object) 按一下切換來源

請參閱 as_json

# File ext/json/lib/json/add/struct.rb, line 9
def self.json_create(object)
  new(*object['v'])
end
StructClass::keyword_init? → true 或 false 值 按一下切換來源

如果類別使用 keyword_init: true 初始化,則傳回 true。否則傳回 nilfalse

範例

Foo = Struct.new(:a)
Foo.keyword_init? # => nil
Bar = Struct.new(:a, keyword_init: true)
Bar.keyword_init? # => true
Baz = Struct.new(:a, keyword_init: false)
Baz.keyword_init? # => false
static VALUE
rb_struct_s_keyword_init_p(VALUE obj)
{
}
StructClass::members → 符號陣列 按一下切換來源

傳回 Struct 後代的成員名稱,為陣列

Customer = Struct.new(:name, :address, :zip)
Customer.members # => [:name, :address, :zip]
static VALUE
rb_struct_s_members_m(VALUE klass)
{
    VALUE members = rb_struct_s_members(klass);

    return rb_ary_dup(members);
}
new(*member_names, keyword_init: nil){|Struct_subclass| ... } → Struct_subclass 按一下切換來源
new(class_name, *member_names, keyword_init: nil){|Struct_subclass| ... } → Struct_subclass
new(*member_names) → Struct_subclass_instance
new(**member_names) → Struct_subclass_instance

Struct.new 傳回 Struct 的新子類別。新子類別

  • 可能是匿名的,或可能具有 class_name 給定的名稱。

  • 可能具有 member_names 給定的成員。

  • 可能透過一般引數或關鍵字引數初始化

新子類別有其自己的方法 ::new;因此

Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo
f = Foo.new(0, 1)                   # => #<struct Struct::Foo foo=0, bar=1>

類別名稱

使用字串引數 class_name,傳回名為 Struct::class_nameStruct 新子類別

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"

成員名稱

Symbol 引數 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
    
    # Initialization with keyword arguments:
    Foo.new(foo: 0)         # => #<struct Struct::Foo foo=0, bar=nil>
    Foo.new(foo: 0, bar: 1) # => #<struct Struct::Foo foo=0, bar=1>
    Foo.new(foo: 0, bar: 1, baz: 2)
    # Raises ArgumentError: unknown keywords: baz
    
  • 方法 :inspect 傳回子類別的字串表示。

    Foo.inspect
    # => "Struct::Foo"
    
  • 方法 ::members 傳回成員名稱的陣列

    Foo.members # => [:foo, :bar]
    

關鍵字引數

預設情況下,初始化新子類別實例的引數可以是位置引數和關鍵字引數。

選擇性關鍵字引數 keyword_init: 允許強制只接受一種類型的引數

KeywordsOnly = Struct.new(:foo, :bar, keyword_init: true)
KeywordsOnly.new(bar: 1, foo: 0)
# => #<struct KeywordsOnly foo=0, bar=1>
KeywordsOnly.new(0, 1)
# Raises ArgumentError: wrong number of arguments

PositionalOnly = Struct.new(:foo, :bar, keyword_init: false)
PositionalOnly.new(0, 1)
# => #<struct PositionalOnly foo=0, bar=1>
PositionalOnly.new(bar: 1, foo: 0)
# => #<struct PositionalOnly foo={:foo=>1, :bar=>2}, bar=nil>
# Note that no error is raised, but arguments treated as one hash value

# Same as not providing keyword_init:
Any = Struct.new(:foo, :bar, keyword_init: nil)
Any.new(foo: 1, bar: 2)
# => #<struct Any foo=1, bar=2>
Any.new(1, 2)
# => #<struct Any foo=1, bar=2>
static VALUE
rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
{
    VALUE name = Qnil, rest, keyword_init = Qnil;
    long i;
    VALUE st;
    VALUE opt;

    argc = rb_scan_args(argc, argv, "0*:", NULL, &opt);
    if (argc >= 1 && !SYMBOL_P(argv[0])) {
        name = argv[0];
        --argc;
        ++argv;
    }

    if (!NIL_P(opt)) {
        static ID keyword_ids[1];

        if (!keyword_ids[0]) {
            keyword_ids[0] = rb_intern("keyword_init");
        }
        rb_get_kwargs(opt, keyword_ids, 0, 1, &keyword_init);
        if (UNDEF_P(keyword_init)) {
            keyword_init = Qnil;
        }
        else if (RTEST(keyword_init)) {
            keyword_init = Qtrue;
        }
    }

    rest = rb_ident_hash_new();
    RBASIC_CLEAR_CLASS(rest);
    for (i=0; i<argc; i++) {
        VALUE mem = rb_to_symbol(argv[i]);
        if (rb_is_attrset_sym(mem)) {
            rb_raise(rb_eArgError, "invalid struct member: %"PRIsVALUE, mem);
        }
        if (RTEST(rb_hash_has_key(rest, mem))) {
            rb_raise(rb_eArgError, "duplicate member: %"PRIsVALUE, mem);
        }
        rb_hash_aset(rest, mem, Qtrue);
    }
    rest = rb_hash_keys(rest);
    RBASIC_CLEAR_CLASS(rest);
    OBJ_FREEZE_RAW(rest);
    if (NIL_P(name)) {
        st = anonymous_struct(klass);
    }
    else {
        st = new_struct(name, klass);
    }
    setup_struct(st, rest);
    rb_ivar_set(st, id_keyword_init, keyword_init);
    if (rb_block_given_p()) {
        rb_mod_module_eval(0, 0, st);
    }

    return st;
}

公開實例方法

self == other → true 或 false 按一下以切換來源

如果且僅如果下列條件為真,則傳回 true;否則傳回 false

  • other.class == self.class.

  • 對於每個成員名稱 nameother.name == self.name

範例

Customer = Struct.new(:name, :address, :zip)
joe    = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe_jr == joe # => true
joe_jr[:name] = 'Joe Smith, Jr.'
# => "Joe Smith, Jr."
joe_jr == joe # => false
static VALUE
rb_struct_equal(VALUE s, VALUE s2)
{
    if (s == s2) return Qtrue;
    if (!RB_TYPE_P(s2, T_STRUCT)) return Qfalse;
    if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
    if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
        rb_bug("inconsistent struct"); /* should never happen */
    }

    return rb_exec_recursive_paired(recursive_equal, s, s2, s2);
}
struct[name] → 物件 按一下以切換來源
struct[n] → 物件

傳回 self 中的值。

如果給定符號或字串引數 name,則傳回命名成員的值

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe[:zip] # => 12345

如果 name 不是成員的名稱,則引發 NameError

如果給定整數引數 n,則在 n 在範圍內時傳回 self.values[n];請參閱 Array 中的陣列索引

joe[2]  # => 12345
joe[-2] # => "123 Maple, Anytown NC"

如果 n 超出範圍,則引發 IndexError

VALUE
rb_struct_aref(VALUE s, VALUE idx)
{
    int i = rb_struct_pos(s, &idx);
    if (i < 0) invalid_struct_pos(s, idx);
    return RSTRUCT_GET(s, i);
}
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 個成員;請參閱 Array 中的陣列索引

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

VALUE
rb_struct_aset(VALUE s, VALUE idx, VALUE val)
{
    int i = rb_struct_pos(s, &idx);
    if (i < 0) invalid_struct_pos(s, idx);
    rb_struct_modify(s);
    RSTRUCT_SET(s, i, val);
    return val;
}
as_json(*) 按一下以切換來源

方法 Struct#as_jsonStruct.json_create 可用於序列化和反序列化 Struct 物件;請參閱 Marshal

方法 Struct#as_json 會序列化 self,傳回表示 self 的 2 元素雜湊

require 'json/add/struct'
Customer = Struct.new('Customer', :name, :address, :zip)
x = Struct::Customer.new.as_json
# => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]}

方法 JSON.create 會反序列化此類雜湊,傳回 Struct 物件

Struct::Customer.json_create(x)
# => #<struct Struct::Customer name=nil, address=nil, zip=nil>
# File ext/json/lib/json/add/struct.rb, line 30
def as_json(*)
  klass = self.class.name
  klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
  {
    JSON.create_id => klass,
    'v'            => values,
  }
end
deconstruct
別名:to_a
deconstruct_keys(array_of_names) → 雜湊 按一下以切換來源

傳回給定成員名稱的名稱/值配對的雜湊。

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
h = joe.deconstruct_keys([:zip, :address])
h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}

如果 array_of_namesnil,則傳回所有名稱和值

h = joe.deconstruct_keys(nil)
h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
static VALUE
rb_struct_deconstruct_keys(VALUE s, VALUE keys)
{
    VALUE h;
    long i;

    if (NIL_P(keys)) {
        return rb_struct_to_h(s);
    }
    if (UNLIKELY(!RB_TYPE_P(keys, T_ARRAY))) {
        rb_raise(rb_eTypeError,
                 "wrong argument type %"PRIsVALUE" (expected Array or nil)",
                 rb_obj_class(keys));

    }
    if (RSTRUCT_LEN(s) < RARRAY_LEN(keys)) {
        return rb_hash_new_with_size(0);
    }
    h = rb_hash_new_with_size(RARRAY_LEN(keys));
    for (i=0; i<RARRAY_LEN(keys); i++) {
        VALUE key = RARRAY_AREF(keys, i);
        int i = rb_struct_pos(s, &key);
        if (i < 0) {
            return h;
        }
        rb_hash_aset(h, key, RSTRUCT_GET(s, i));
    }
    return h;
}
dig(name, *identifiers) → 物件 按一下以切換來源
dig(n, *identifiers) → 物件

在巢狀物件中尋找並傳回物件。巢狀物件可能是各種類別的實例。請參閱 Dig 方法

給定符號或字串引數 name,傳回由 nameidentifiers 指定的物件

Foo = Struct.new(:a)
f = Foo.new(Foo.new({b: [1, 2, 3]}))
f.dig(:a) # => #<struct Foo a={:b=>[1, 2, 3]}>
f.dig(:a, :a) # => {:b=>[1, 2, 3]}
f.dig(:a, :a, :b) # => [1, 2, 3]
f.dig(:a, :a, :b, 0) # => 1
f.dig(:b, 0) # => nil

給定整數引數 n,傳回由 nidentifiers 指定的物件

f.dig(0) # => #<struct Foo a={:b=>[1, 2, 3]}>
f.dig(0, 0) # => {:b=>[1, 2, 3]}
f.dig(0, 0, :b) # => [1, 2, 3]
f.dig(0, 0, :b, 0) # => 1
f.dig(:b, 0) # => nil
static VALUE
rb_struct_dig(int argc, VALUE *argv, VALUE self)
{
    rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
    self = rb_struct_lookup(self, *argv);
    if (!--argc) return self;
    ++argv;
    return rb_obj_dig(argc, argv, self, Qnil);
}
each {|value| ... } → self 按一下以切換來源
each → enumerator

使用每個成員的值呼叫給定的區塊;傳回 self

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.each {|value| p value }

輸出

"Joe Smith"
"123 Maple, Anytown NC"
12345

如果未給定區塊,傳回 Enumerator

相關:each_pair

static VALUE
rb_struct_each(VALUE s)
{
    long i;

    RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
    for (i=0; i<RSTRUCT_LEN(s); i++) {
        rb_yield(RSTRUCT_GET(s, i));
    }
    return s;
}
each_pair {|(name, value)| ... } → self 按一下以切換來源
each_pair → enumerator

使用每個成員名稱/值對呼叫給定的區塊;傳回 self

Customer = Struct.new(:name, :address, :zip) # => Customer
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.each_pair {|(name, value)| p "#{name} => #{value}" }

輸出

"name => Joe Smith"
"address => 123 Maple, Anytown NC"
"zip => 12345"

如果未給定區塊,傳回 Enumerator

相關:each

static VALUE
rb_struct_each_pair(VALUE s)
{
    VALUE members;
    long i;

    RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
    members = rb_struct_members(s);
    if (rb_block_pair_yield_optimizable()) {
        for (i=0; i<RSTRUCT_LEN(s); i++) {
            VALUE key = rb_ary_entry(members, i);
            VALUE value = RSTRUCT_GET(s, i);
            rb_yield_values(2, key, value);
        }
    }
    else {
        for (i=0; i<RSTRUCT_LEN(s); i++) {
            VALUE key = rb_ary_entry(members, i);
            VALUE value = RSTRUCT_GET(s, i);
            rb_yield(rb_assoc_new(key, value));
        }
    }
    return s;
}
eql?(other) → true 或 false 按一下以切換來源

如果且僅如果下列條件為真,則傳回 true;否則傳回 false

  • other.class == self.class.

  • 對於每個成員名稱 nameother.name.eql?(self.name)

    Customer = Struct.new(:name, :address, :zip)
    joe    = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
    joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
    joe_jr.eql?(joe) # => true
    joe_jr[:name] = 'Joe Smith, Jr.'
    joe_jr.eql?(joe) # => false
    

相關:Object#==

static VALUE
rb_struct_eql(VALUE s, VALUE s2)
{
    if (s == s2) return Qtrue;
    if (!RB_TYPE_P(s2, T_STRUCT)) return Qfalse;
    if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
    if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
        rb_bug("inconsistent struct"); /* should never happen */
    }

    return rb_exec_recursive_paired(recursive_eql, s, s2, s2);
}
filter
別名:select
hash → 整數 按一下以切換來源

傳回 self 的整數雜湊值。

同類別且內容相同的兩個結構會具有相同的雜湊碼(並會使用 Struct#eql? 進行比較)

Customer = Struct.new(:name, :address, :zip)
joe    = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.hash == joe_jr.hash # => true
joe_jr[:name] = 'Joe Smith, Jr.'
joe.hash == joe_jr.hash # => false

相關:Object#hash

static VALUE
rb_struct_hash(VALUE s)
{
    long i, len;
    st_index_t h;
    VALUE n;

    h = rb_hash_start(rb_hash(rb_obj_class(s)));
    len = RSTRUCT_LEN(s);
    for (i = 0; i < len; i++) {
        n = rb_hash(RSTRUCT_GET(s, i));
        h = rb_hash_uint(h, NUM2LONG(n));
    }
    h = rb_hash_end(h);
    return ST2FIX(h);
}
inspect → 字串 按一下以切換來源

傳回 self 的字串表示。

Customer = Struct.new(:name, :address, :zip) # => Customer
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
static VALUE
rb_struct_inspect(VALUE s)
{
    return rb_exec_recursive(inspect_struct, s, rb_str_new2("#<struct "));
}
別名:to_s
length
別名:size
members → 符號陣列 按一下以切換來源

傳回 self 的成員名稱,形式為陣列

Customer = Struct.new(:name, :address, :zip)
Customer.new.members # => [:name, :address, :zip]

相關:to_a

static VALUE
rb_struct_members_m(VALUE obj)
{
    return rb_struct_s_members_m(rb_obj_class(obj));
}
select {|value| ... } → 陣列 按一下以切換來源
select → enumerator

如果給定區塊,傳回 self 中區塊傳回真值的值的陣列

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
a = joe.select {|value| value.is_a?(String) }
a # => ["Joe Smith", "123 Maple, Anytown NC"]
a = joe.select {|value| value.is_a?(Integer) }
a # => [12345]

如果未給定區塊,傳回 Enumerator

static VALUE
rb_struct_select(int argc, VALUE *argv, VALUE s)
{
    VALUE result;
    long i;

    rb_check_arity(argc, 0, 0);
    RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
    result = rb_ary_new();
    for (i = 0; i < RSTRUCT_LEN(s); i++) {
        if (RTEST(rb_yield(RSTRUCT_GET(s, i)))) {
            rb_ary_push(result, RSTRUCT_GET(s, i));
        }
    }

    return result;
}
別名:filter
size → 整數 按一下以切換來源

傳回成員數目。

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.size #=> 3
VALUE
rb_struct_size(VALUE s)
{
    return LONG2FIX(RSTRUCT_LEN(s));
}
別名:length
to_a → 陣列 按一下以切換來源

傳回 self 中的值為陣列

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]

相關:members

static VALUE
rb_struct_to_a(VALUE s)
{
    return rb_ary_new4(RSTRUCT_LEN(s), RSTRUCT_CONST_PTR(s));
}
別名為:valuesdeconstruct
to_h → hash 按一下以切換來源
to_h {|name, value| ... } → hash

傳回包含每個成員的名稱和值的 hash

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
h = joe.to_h
h # => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}

如果給定區塊,則使用每個名稱/值配對呼叫它;區塊應傳回 2 個元素的陣列,其元素將成為傳回 hash 中的鍵值配對

h = joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}
h # => {:NAME=>"JOE SMITH", :ADDRESS=>"123 MAPLE, ANYTOWN NC", :ZIP=>"12345"}

如果區塊傳回不適當的值,則會引發 ArgumentError

static VALUE
rb_struct_to_h(VALUE s)
{
    VALUE h = rb_hash_new_with_size(RSTRUCT_LEN(s));
    VALUE members = rb_struct_members(s);
    long i;
    int block_given = rb_block_given_p();

    for (i=0; i<RSTRUCT_LEN(s); i++) {
        VALUE k = rb_ary_entry(members, i), v = RSTRUCT_GET(s, i);
        if (block_given)
            rb_hash_set_pair(h, rb_yield_values(2, k, v));
        else
            rb_hash_aset(h, k, v);
    }
    return h;
}
to_json(*args) 按一下以切換來源

傳回表示 selfJSON 字串

require 'json/add/struct'
Customer = Struct.new('Customer', :name, :address, :zip)
puts Struct::Customer.new.to_json

輸出

{"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
# File ext/json/lib/json/add/struct.rb, line 49
def to_json(*args)
  as_json.to_json(*args)
end
to_s
別名為:inspect
values
別名:to_a
values_at(*integers) → 陣列 按一下以切換來源
values_at(integer_range) → 陣列

傳回 self 的值陣列。

給定整數引數 integers 時,傳回包含 integers 中一個給定值的陣列

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.values_at(0, 2)    # => ["Joe Smith", 12345]
joe.values_at(2, 0)    # => [12345, "Joe Smith"]
joe.values_at(2, 1, 0) # => [12345, "123 Maple, Anytown NC", "Joe Smith"]
joe.values_at(0, -3)   # => ["Joe Smith", "Joe Smith"]

如果任何 integers 超出範圍,則會引發 IndexError;請參閱 Array 中的陣列索引

給定整數範圍引數 integer_range 時,傳回包含範圍元素給定值的陣列;對於大於結構的範圍元素,以 nil 值填滿

joe.values_at(0..2)
# => ["Joe Smith", "123 Maple, Anytown NC", 12345]
joe.values_at(-3..-1)
# => ["Joe Smith", "123 Maple, Anytown NC", 12345]
joe.values_at(1..4) # => ["123 Maple, Anytown NC", 12345, nil, nil]

如果範圍的任何元素為負且超出範圍,則會引發 RangeError;請參閱 Array 中的陣列索引

static VALUE
rb_struct_values_at(int argc, VALUE *argv, VALUE s)
{
    return rb_get_values_at(s, RSTRUCT_LEN(s), argc, argv, struct_entry);
}