Spec-Zone.ru › Ruby 2.7

класс Struct

Родитель:
Объект
Включенные модули:
Enumerable

A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.

The Struct class generates new subclasses that hold a set of members and their values. For each member a reader and writer method is created similar to Module#attr_accessor.

Customer = Struct.new(:name, :address) do
  def greeting
    "Hello #{name}!"
  end
end

dave = Customer.new("Dave", "123 Main")
dave.name     #=> "Dave"
dave.greeting #=> "Hello Dave!"

See Struct::new for further examples of creating struct subclasses and instances.

In the method descriptions that follow, a “member” parameter refers to a struct member which is either a quoted string ("name") or a Symbol (:name).

Константы

Group

Group

Group is a Struct that is only available when compiled with HAVE_GETGRENT.

The struct contains the following members:

name

contains the name of the group as a String.

passwd

contains the encrypted password as a String. An 'x' is returned if password access to the group is not available; an empty string is returned if no password is needed to obtain membership of the group.

Must be compiled with HAVE_STRUCT_GROUP_GR_PASSWD.

gid

contains the group's numeric ID as an integer.

mem

is an Array of Strings containing the short login names of the members of the group.

Passwd

Passwd

Passwd is a Struct that contains the following members:

name

contains the short login name of the user as a String.

passwd

contains the encrypted password of the user as a String. an 'x' is returned if shadow passwords are in use. An '*' is returned if the user cannot log in using a password.

uid

contains the integer user ID (uid) of the user.

gid

contains the integer group ID (gid) of the user's primary group.

dir

contains the path to the home directory of the user as a String.

shell

contains the path to the login shell of the user as a String.

Дополнительные члены (необязательные), требующие специальных флагов при компиляции:

gecos

contains a longer String description of the user, such as a full name. Some Unix systems provide structured information in the gecos field, but this is system-dependent. must be compiled with HAVE_STRUCT_PASSWD_PW_GECOS

change

password change time(integer) must be compiled with HAVE_STRUCT_PASSWD_PW_CHANGE

quota

quota value(integer) must be compiled with HAVE_STRUCT_PASSWD_PW_QUOTA

age

password age(integer) must be compiled with HAVE_STRUCT_PASSWD_PW_AGE

class

user access class(string) must be compiled with HAVE_STRUCT_PASSWD_PW_CLASS

comment

comment(string) must be compiled with HAVE_STRUCT_PASSWD_PW_COMMENT

expire

account expiration time(integer) must be compiled with HAVE_STRUCT_PASSWD_PW_EXPIRE

Tms

Tms для обратной совместимости

Публичные методы класса

json_create(object) Показать исходный код
# File ext/json/lib/json/add/struct.rb, line 10
def self.json_create(object)
  new(*object['v'])
end

Десериализует строку JSON, создавая новый объект Struct со значениями v, сериализованными с помощью to_json.

new([class_name] [, member_name]+) → StructClass Показать исходный код
new([class_name] [, member_name]+, keyword_init: true) → StructClass
new([class_name] [, member_name]+) {|StructClass| block } → StructClass
new(value, ...) → object
StructClass[value, ...] → object
static VALUE
rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
{
    VALUE name, rest, keyword_init = Qfalse;
    long i;
    VALUE st;
    st_table *tbl;

    rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
    name = argv[0];
    if (SYMBOL_P(name)) {
        name = Qnil;
    }
    else {
        --argc;
        ++argv;
    }

    if (RB_TYPE_P(argv[argc-1], T_HASH)) {
        static ID keyword_ids[1];

        if (!keyword_ids[0]) {
            keyword_ids[0] = rb_intern("keyword_init");
        }
        rb_get_kwargs(argv[argc-1], keyword_ids, 0, 1, &keyword_init);
        if (keyword_init == Qundef) {
            keyword_init = Qfalse;
        }
        --argc;
    }

    rest = rb_ident_hash_new();
    RBASIC_CLEAR_CLASS(rest);
    tbl = RHASH_TBL(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 (st_insert(tbl, mem, Qtrue)) {
            rb_raise(rb_eArgError, "duplicate member: %"PRIsVALUE, mem);
        }
    }
    rest = rb_hash_keys(rest);
    st_clear(tbl);
    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;
}

Первые два формата используются для создания нового подкласса Struct, class_name, который может содержать значение для каждого member_name. Этот подкласс может быть использован для создания экземпляров структуры, как любой другой Class.

Если class_name опущено, будет создан анонимный класс структуры. В противном случае имя этой структуры будет отображено как константа в классе Struct, поэтому оно должно быть уникальным для всех структур в системе и начинаться с заглавной буквы. Назначение класса структуры константе также присваивает классу имя константы.

# Create a structure with a name under Struct
Struct.new("Customer", :name, :address)
#=> Struct::Customer
Struct::Customer.new("Dave", "123 Main")
#=> #<struct Struct::Customer name="Dave", address="123 Main">

# Create a structure named by its constant
Customer = Struct.new(:name, :address)
#=> Customer
Customer.new("Dave", "123 Main")
#=> #<struct Customer name="Dave", address="123 Main">

Если необязательный ключевой аргумент keyword_init установлен в true, .new принимает ключевые аргументы вместо обычных.

Customer = Struct.new(:name, :address, keyword_init: true)
Customer.new(name: "Dave", address: "123 Main")
#=> #<struct Customer name="Dave", address="123 Main">

Если задан блок, он будет выполнен в контексте StructClass, передав созданный класс в качестве параметра:

Customer = Struct.new(:name, :address) do
  def greeting
    "Hello #{name}!"
  end
end
Customer.new("Dave", "123 Main").greeting  #=> "Hello Dave!"

Это рекомендуемый способ настройки структуры. Наследование от анонимной структуры создает дополнительный анонимный класс, который никогда не будет использован.

Последние два формата создают новый экземпляр подкласса структуры. Количество value параметров должно быть меньше или равно количеству атрибутов, определенных для структуры. Неуказанные параметры по умолчанию равны nil. Передача большего числа параметров, чем атрибутов, вызовет ArgumentError.

Customer = Struct.new(:name, :address)
Customer.new("Dave", "123 Main")
#=> #<struct Customer name="Dave", address="123 Main">
Customer["Dave"]
#=> #<struct Customer name="Dave", address=nil>

Публичные методы экземпляров

struct == other → true или 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);
}

Равенство — Возвращает true если other имеет тот же подкласс структуры и имеет равные значения членов (в соответствии с Object#==).

Customer = Struct.new(:name, :address, :zip)
joe   = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
jane  = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
joe == joejr   #=> true
joe == jane    #=> false
struct[member] → объект Показать исходный код
struct[index] → объект
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);
}

Ссылка на атрибут — Возвращает значение заданного члена структуры member или члена по заданному index. Вызывает NameError, если member не существует, и IndexError, если index находится вне диапазона.

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

joe["name"]   #=> "Joe Smith"
joe[:name]    #=> "Joe Smith"
joe[0]        #=> "Joe Smith"
struct[member] = obj → obj Показать исходный код
struct[index] = obj → obj
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;
}

Присваивание атрибута — Устанавливает значение заданного члена структуры member или члена по заданному index. Вызывает NameError, если member не существует, и IndexError, если index находится вне диапазона.

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

joe["name"] = "Luke"
joe[:zip]   = "90210"

joe.name   #=> "Luke"
joe.zip    #=> "90210"
as_json(*) Показать исходный код
# File ext/json/lib/json/add/struct.rb, line 16
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

Возвращает хеш, который будет преобразован в объект JSON и представлять этот объект.

to_a → массив Показать исходный код
values → массив
static VALUE
rb_struct_to_a(VALUE s)
{
    return rb_ary_new4(RSTRUCT_LEN(s), RSTRUCT_CONST_PTR(s));
}

Возвращает значения для этой структуры в виде массива Array.

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.to_a[1]   #=> "123 Maple, Anytown NC"
deconstruct_keys(p1) Показать исходный код
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(key, ...) → объект Показать исходный код
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);
}

Извлекает вложенное значение, заданное последовательностью key объектов, вызывая dig на каждом шаге, возвращая nil , если любой промежуточный шаг nil.

Foo = Struct.new(:a)
f = Foo.new(Foo.new({b: [1, 2, 3]}))

f.dig(:a, :a, :b, 0)    # => 1
f.dig(:b, 0)            # => nil
f.dig(:a, :a, :b, :c)   # TypeError: no implicit conversion of Symbol into Integer
each {|obj| блок } → структура Показать исходный код
each → перечислитель
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;
}

Возвращает значение каждого члена структуры в порядке. Если блок не задан, возвращается перечислитель.

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

Результат:

Joe Smith
123 Maple, Anytown NC
12345
each_pair {|sym, obj| блок } → структура Показать исходный код
each_pair → перечислитель
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_arity() > 1) {
        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;
}

Возвращает имя и значение каждого члена структуры в порядке. Если блок не задан, возвращается перечислитель.

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

Результат:

name => Joe Smith
address => 123 Maple, Anytown NC
zip => 12345
eql?(other) → true или false Показать исходный код
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);
}

Равенство хешей — Hash и struct относятся к одному и тому же ключу хеша, если они имеют тот же подкласс структуры и имеют равные значения членов (согласно Object#eql?).

filter {|obj| блок } → массив Показать исходный код
filter → перечислитель
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;
}

Возвращает каждое значение члена структуры в блок и возвращает массив Array, содержащий значения членов структуры, для которых заданный блок возвращает значение true (эквивалентно Enumerable#select).

Lots = Struct.new(:a, :b, :c, :d, :e, :f)
l = Lots.new(11, 22, 33, 44, 55, 66)
l.select {|v| v.even? }   #=> [22, 44, 66]

Struct#filter является псевдонимом для Struct#select.

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);
}

Возвращает значение хеша на основе содержимого этой структуры.

См. также Object#hash.

to_s → строка Показать исходный код
inspect → строка
static VALUE
rb_struct_inspect(VALUE s)
{
    return rb_exec_recursive(inspect_struct, s, 0);
}

Возвращает описание этой структуры в виде строки.

Также является псевдонимом: to_s
length → целое число Показать исходный код
VALUE
rb_struct_size(VALUE s)
{
    return LONG2FIX(RSTRUCT_LEN(s));
}

Возвращает количество членов структуры.

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.length   #=> 3
members → массив Показать исходный код
static VALUE
rb_struct_members_m(VALUE obj)
{
    return rb_struct_s_members_m(rb_obj_class(obj));
}

Возвращает члены структуры как массив символов:

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.members   #=> [:name, :address, :zip]
select {|obj| блок } → массив Показать исходный код
select → перечислитель
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;
}

Возвращает каждое значение члена структуры в блок и возвращает массив Array, содержащий значения членов структуры, для которых заданный блок возвращает значение true (эквивалентно Enumerable#select).

Lots = Struct.new(:a, :b, :c, :d, :e, :f)
l = Lots.new(11, 22, 33, 44, 55, 66)
l.select {|v| v.even? }   #=> [22, 44, 66]

Struct#filter является псевдонимом для Struct#select.

size → целое число Показать исходный код
VALUE
rb_struct_size(VALUE s)
{
    return LONG2FIX(RSTRUCT_LEN(s));
}

Возвращает количество членов структуры.

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.length   #=> 3
to_a → массив Показать исходный код
static VALUE
rb_struct_to_a(VALUE s)
{
    return rb_ary_new4(RSTRUCT_LEN(s), RSTRUCT_CONST_PTR(s));
}

Возвращает значения для этой структуры в виде массива Array.

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.to_a[1]   #=> "123 Maple, Anytown NC"
to_h → хеш Показать исходный код
to_h {|name, value| блок } → хеш
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;
}

Возвращает хеш Hash, содержащий имена и значения для членов структуры.

Если задан блок, результаты выполнения блока для каждой пары получателя будут использоваться в качестве пар.

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.to_h[:address]   #=> "123 Maple, Anytown NC"
joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}[:ADDRESS]
                     #=> "123 MAPLE, ANYTOWN NC"
to_json(*args) Показать исходный код
# File ext/json/lib/json/add/struct.rb, line 27
def to_json(*args)
  as_json.to_json(*args)
end

Сохраняет имя класса (Struct) со значениями Struct v в виде строки JSON. Поддерживаются только именованные структуры.

to_s()
Alias for: inspect
values → array Показать исходный код
static VALUE
rb_struct_to_a(VALUE s)
{
    return rb_ary_new4(RSTRUCT_LEN(s), RSTRUCT_CONST_PTR(s));
}

Возвращает значения для этой структуры в виде Array.

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.to_a[1]   #=> "123 Maple, Anytown NC"
values_at(selector, ...) → 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);
}

Возвращает значения членов структуры для каждого selector в виде Array. selector может быть либо смещением Integer, либо Range смещений (как в Array#values_at).

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

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API