Class: Struct

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

Instance Method Summary collapse

Constructor Details

#initializeObject

15.2.18.4.8



344
345
346
347
348
349
350
351
352
# File 'mrbgems/mruby-struct/src/struct.c', line 344

static mrb_value
mrb_struct_initialize(mrb_state *mrb, mrb_value self)
{
  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)


545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'mrbgems/mruby-struct/src/struct.c', line 545

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

  mrb_get_args(mrb, "o", &s2);
  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();
  }
  if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
    mrb_bug(mrb, "inconsistent struct"); /* should never happen */
  }
  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:

  • #[](symbol) ⇒ Object

    Returns:

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

    Returns:

    • (Object)


428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'mrbgems/mruby-struct/src/struct.c', line 428

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

  mrb_get_args(mrb, "o", &idx);
  if (mrb_string_p(idx)) {
    mrb_value sym = mrb_check_intern_str(mrb, idx);

    if (mrb_nil_p(sym)) {
      mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%v' in struct", idx);
    }
    idx = sym;
  }
  if (mrb_symbol_p(idx)) {
    return struct_aref_sym(mrb, s, mrb_symbol(idx));
  }
  return struct_aref_int(mrb, s, mrb_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:

  • #[]=(symbol) ⇒ Object

    Returns:

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

    Returns:

    • (Object)


492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'mrbgems/mruby-struct/src/struct.c', line 492

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_value sym = mrb_check_intern_str(mrb, idx);

    if (mrb_nil_p(sym)) {
      mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%v' in struct", idx);
    }
    idx = sym;
  }
  if (mrb_symbol_p(idx)) {
    return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val);
  }

  i = mrb_int(mrb, idx);
  if (i < 0) i = RSTRUCT_LEN(s) + i;
  if (i < 0) {
    mrb_raisef(mrb, E_INDEX_ERROR,
               "offset %i too small for struct(size:%i)", i, RSTRUCT_LEN(s));
  }
  if (RSTRUCT_LEN(s) <= i) {
    mrb_raisef(mrb, E_INDEX_ERROR,
               "offset %i too large for struct(size:%i)", i, RSTRUCT_LEN(s));
  }
  mrb_struct_modify(mrb, s);
  return RSTRUCT_PTR(s)[i] = val;
}

#_inspect(recur_list) ⇒ Object



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

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+.



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

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



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

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 respectiev element.

ISO 15.2.18.4.5



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

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

#eql?Boolean

code-seq: struct.eql?(other) -> true or false

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

Returns:

  • (Boolean)


582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'mrbgems/mruby-struct/src/struct.c', line 582

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

  mrb_get_args(mrb, "o", &s2);
  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();
  }
  if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
    mrb_bug(mrb, "inconsistent struct"); /* should never happen */
  }
  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:



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'mrbgems/mruby-struct/src/struct.c', line 356

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

  mrb_get_args(mrb, "o", &s);

  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_array_p(s)) {
    mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
  }
  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)



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

def inspect
  self._inspect({})
end

#lengthFixnum #sizeFixnum

Returns number of struct members.

Overloads:



618
619
620
621
622
# File 'mrbgems/mruby-struct/src/struct.c', line 618

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:



107
108
109
110
111
# File 'mrbgems/mruby-struct/src/struct.c', line 107

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



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

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

#lengthFixnum #sizeFixnum

Returns number of struct members.

Overloads:



618
619
620
621
622
# File 'mrbgems/mruby-struct/src/struct.c', line 618

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:



631
632
633
634
635
# File 'mrbgems/mruby-struct/src/struct.c', line 631

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:



643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'mrbgems/mruby-struct/src/struct.c', line 643

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:



631
632
633
634
635
# File 'mrbgems/mruby-struct/src/struct.c', line 631

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



659
660
661
662
663
664
665
666
667
668
# File 'mrbgems/mruby-struct/src/struct.c', line 659

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

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

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