Class: Struct

Inherits:
Object show all
Includes:
Enumerable
Defined in:
mruby/mrbgems/mruby-struct/mrblib/struct.rb,
mruby/mrbgems/mruby-struct/src/struct.c

Overview

Struct

ISO 15.2.18

Direct Known Subclasses

Carbuncle::AnimatedSprite::Animation

Constant Summary

Constants included from Enumerable

Enumerable::NONE

Instance Method Summary collapse

Methods included from Enumerable

__update_hash, #all?, #any?, #chain, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #sum, #take, #take_while, #tally, #uniq, #zip

Constructor Details

#initializeObject

15.2.18.4.8



330
331
332
333
334
335
336
337
338
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 330

static mrb_value
mrb_struct_initialize(mrb_state *mrb, mrb_value self)
{
  const mrb_value *argv;
  mrb_int argc;

  mrb_get_args(mrb, "*!", &argv, &argc);
  return mrb_struct_initialize_withArg(mrb, argc, argv, self);
}

Instance Method Details

#==(other_struct) ⇒ Boolean

Equality—Returns true if other_struct is equal to this one: they must be of the same class as generated by Struct::new, and the values of all instance variables must be equal (according to 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

Returns:

  • (Boolean)


520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 520

static mrb_value
mrb_struct_equal(mrb_state *mrb, mrb_value s)
{
  mrb_value s2 = mrb_get_arg1(mrb);
  mrb_value *ptr, *ptr2;
  mrb_int i, len;

  if (mrb_obj_equal(mrb, s, s2)) {
    return mrb_true_value();
  }
  if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
    return mrb_false_value();
  }
  mrb_assert(RSTRUCT_LEN(s) == RSTRUCT_LEN(s2));
  ptr = RSTRUCT_PTR(s);
  ptr2 = RSTRUCT_PTR(s2);
  len = RSTRUCT_LEN(s);
  for (i=0; i<len; i++) {
    if (!mrb_equal(mrb, ptr[i], ptr2[i])) {
      return mrb_false_value();
    }
  }

  return mrb_true_value();
}

#[](symbol) ⇒ Object #[](fixnum) ⇒ Object

Attribute Reference—Returns the value of the instance variable named by symbol, or indexed (0..length-1) by fixnum. Will raise NameError if the named variable does not exist, or IndexError if the index is out of range.

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"

Overloads:



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 419

static mrb_value
mrb_struct_aref(mrb_state *mrb, mrb_value s)
{
  mrb_value idx = mrb_get_arg1(mrb);

  if (mrb_string_p(idx)) {
    mrb_sym sym = mrb_intern_str(mrb, idx);
    idx = mrb_symbol_value(sym);
  }
  if (mrb_symbol_p(idx)) {
    return struct_aref_sym(mrb, s, mrb_symbol(idx));
  }
  return struct_aref_int(mrb, s, mrb_as_int(mrb, idx));
}

#[]=(symbol) ⇒ Object #[]=(fixnum) ⇒ Object

Attribute Assignment—Assigns to the instance variable named by symbol or fixnum the value obj and returns it. Will raise a NameError if the named variable does not exist, or an IndexError if the index is out of range.

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"

Overloads:



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 479

static mrb_value
mrb_struct_aset(mrb_state *mrb, mrb_value s)
{
  mrb_int i;
  mrb_value idx;
  mrb_value val;

  mrb_get_args(mrb, "oo", &idx, &val);

  if (mrb_string_p(idx)) {
    mrb_sym sym = mrb_intern_str(mrb, idx);
    idx = mrb_symbol_value(sym);
  }
  if (mrb_symbol_p(idx)) {
    return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val);
  }

  i = struct_index(mrb, mrb_as_int(mrb, idx), RSTRUCT_LEN(s));
  mrb_struct_modify(mrb, s);
  mrb_field_write_barrier_value(mrb, mrb_basic_ptr(s), val);
  return RSTRUCT_PTR(s)[i] = val;
}

#_inspect(recur_list) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'mruby/mrbgems/mruby-struct/mrblib/struct.rb', line 48

def _inspect(recur_list)
  return "#<struct #{self.class}:...>" if recur_list[self.object_id]
  recur_list[self.object_id] = true
  name = self.class.to_s
  if name[0] == "#"
    str = "#<struct "
  else
    str = "#<struct #{name} "
  end
  buf = []
  self.each_pair do |k,v|
    buf.push k.to_s + "=" + v._inspect(recur_list)
  end
  str + buf.join(", ") + ">"
end

#dig(idx, *args) ⇒ Object

call-seq:

hsh.dig(key,...)                 -> object

Extracts the nested value specified by the sequence of key objects by calling dig at each step, returning nil if any intermediate step is nil.



90
91
92
93
94
95
96
97
# File 'mruby/mrbgems/mruby-struct/mrblib/struct.rb', line 90

def dig(idx,*args)
  n = self[idx]
  if args.size > 0
    n&.dig(*args)
  else
    n
  end
end

#each(&block) ⇒ Object

Calls the given block for each element of self and pass the respective element.

ISO 15.2.18.4.4



13
14
15
16
17
18
# File 'mruby/mrbgems/mruby-struct/mrblib/struct.rb', line 13

def each(&block)
  self.class.members.each{|field|
    block.call(self[field])
  }
  self
end

#each_pair(&block) ⇒ Object

Calls the given block for each element of self and pass the name and value of the respective element.

ISO 15.2.18.4.5



26
27
28
29
30
31
# File 'mruby/mrbgems/mruby-struct/mrblib/struct.rb', line 26

def each_pair(&block)
  self.class.members.each{|field|
    block.call(field.to_sym, self[field])
  }
  self
end

#eql?(other) ⇒ Boolean

Two structures are equal if they are the same object, or if all their fields are equal (using eql?).

Returns:

  • (Boolean)


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 554

static mrb_value
mrb_struct_eql(mrb_state *mrb, mrb_value s)
{
  mrb_value s2 = mrb_get_arg1(mrb);
  mrb_value *ptr, *ptr2;
  mrb_int i, len;

  if (mrb_obj_equal(mrb, s, s2)) {
    return mrb_true_value();
  }
  if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
    return mrb_false_value();
  }
  mrb_assert(RSTRUCT_LEN(s) == RSTRUCT_LEN(s2));
  ptr = RSTRUCT_PTR(s);
  ptr2 = RSTRUCT_PTR(s2);
  len = RSTRUCT_LEN(s);
  for (i=0; i<len; i++) {
    if (!mrb_eql(mrb, ptr[i], ptr2[i])) {
      return mrb_false_value();
    }
  }

  return mrb_true_value();
}

#initialize_copyObject

:nodoc:



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 342

static mrb_value
mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
{
  mrb_value s = mrb_get_arg1(mrb);

  if (mrb_obj_equal(mrb, copy, s)) return copy;
  if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) {
    mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
  }
  if (!mrb_struct_p(s)) {
    struct_corrupted(mrb);
  }
  mrb_ary_replace(mrb, copy, s);
  return copy;
}

#inspectObject Also known as: to_s

call-seq:

struct.to_s      -> string
struct.inspect   -> string

Describe the contents of this struct in a string.

15.2.18.4.10(x)



73
74
75
# File 'mruby/mrbgems/mruby-struct/mrblib/struct.rb', line 73

def inspect
  self._inspect({})
end

#lengthInteger #sizeInteger

Returns number of struct members.

Overloads:



587
588
589
590
591
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 587

static mrb_value
mrb_struct_len(mrb_state *mrb, mrb_value self)
{
  return mrb_fixnum_value(RSTRUCT_LEN(self));
}

#membersArray

Returns an array of strings representing the names of the instance variables.

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

Returns:



99
100
101
102
103
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 99

static mrb_value
mrb_struct_members(mrb_state *mrb, mrb_value obj)
{
  return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj)));
}

#select(&block) ⇒ Object

Calls the given block for each element of self and returns an array with all elements of which block is not false.

ISO 15.2.18.4.7



39
40
41
42
43
44
45
46
# File 'mruby/mrbgems/mruby-struct/mrblib/struct.rb', line 39

def select(&block)
  ary = []
  self.class.members.each{|field|
    val = self[field]
    ary.push(val) if block.call(val)
  }
  ary
end

#lengthInteger #sizeInteger

Returns number of struct members.

Overloads:



587
588
589
590
591
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 587

static mrb_value
mrb_struct_len(mrb_state *mrb, mrb_value self)
{
  return mrb_fixnum_value(RSTRUCT_LEN(self));
}

#to_aArray #valuesArray

Create an array from struct values.

Overloads:



600
601
602
603
604
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 600

static mrb_value
mrb_struct_to_a(mrb_state *mrb, mrb_value self)
{
  return mrb_ary_new_from_values(mrb, RSTRUCT_LEN(self), RSTRUCT_PTR(self));
}

#to_hHash

Create a hash from member names and struct values.

Returns:



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 612

static mrb_value
mrb_struct_to_h(mrb_state *mrb, mrb_value self)
{
  mrb_value members, ret;
  mrb_int i;

  members = struct_members(mrb, self);
  ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members));

  for (i = 0; i < RARRAY_LEN(members); i++) {
    mrb_hash_set(mrb, ret, RARRAY_PTR(members)[i], RSTRUCT_PTR(self)[i]);
  }

  return ret;
}

#to_aArray #valuesArray

Create an array from struct values.

Overloads:



600
601
602
603
604
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 600

static mrb_value
mrb_struct_to_a(mrb_state *mrb, mrb_value self)
{
  return mrb_ary_new_from_values(mrb, RSTRUCT_LEN(self), RSTRUCT_PTR(self));
}

#values_atObject



628
629
630
631
632
633
634
635
636
637
# File 'mruby/mrbgems/mruby-struct/src/struct.c', line 628

static mrb_value
mrb_struct_values_at(mrb_state *mrb, mrb_value self)
{
  mrb_int argc;
  const mrb_value *argv;

  mrb_get_args(mrb, "*", &argv, &argc);

  return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int);
}