Class: Struct
- Defined in:
- mrbgems/mruby-struct/src/struct.c,
mrbgems/mruby-struct/mrblib/struct.rb
Instance Method Summary collapse
-
#==(other_struct) ⇒ Boolean
Equality—Returns
trueif other_struct is equal to this one: they must be of the same class as generated byStruct::new, and the values of all instance variables must be equal (according toObject#==). -
#[] ⇒ Object
Attribute Reference—Returns the value of the instance variable named by symbol, or indexed (0..length-1) by fixnum.
-
#[]= ⇒ Object
Attribute Assignment—Assigns to the instance variable named by symbol or fixnum the value obj and returns it.
-
#_inspect ⇒ Object
-
#each(&block) ⇒ Object
Calls the given block for each element of +self+ and pass the respective element.
-
#each_pair(&block) ⇒ Object
Calls the given block for each element of +self+ and pass the name and value of the respectiev element.
-
#eql? ⇒ Boolean
code-seq: struct.eql?(other) -> true or false.
-
#initialize ⇒ Object
constructor
15.2.18.4.8.
-
#initialize_copy ⇒ Object
:nodoc:.
-
#inspect ⇒ Object
(also: #to_s)
call-seq: struct.to_s -> string struct.inspect -> string.
-
#length ⇒ Object
Returns number of struct members.
-
#members ⇒ Array
Returns an array of strings representing the names of the instance variables.
-
#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.
-
#size ⇒ Object
Returns number of struct members.
-
#to_a ⇒ Object
Create an array from struct values.
-
#to_h ⇒ Hash
Create a hash from member names and struct values.
-
#values ⇒ Object
Create an array from struct values.
-
#values_at ⇒ Object
Constructor Details
#initialize ⇒ Object
15.2.18.4.8
354 355 356 357 358 359 360 361 362 |
# File 'mrbgems/mruby-struct/src/struct.c', line 354 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
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'mrbgems/mruby-struct/src/struct.c', line 558 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”
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'mrbgems/mruby-struct/src/struct.c', line 439 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 '%S' 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”
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'mrbgems/mruby-struct/src/struct.c', line 503 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 '%S' 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 %S too small for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } if (RSTRUCT_LEN(s) <= i) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too large for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } mrb_struct_modify(mrb, s); return RSTRUCT_PTR(s)[i] = val; } |
#_inspect ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'mrbgems/mruby-struct/mrblib/struct.rb', line 49 def _inspect 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] end str + buf.join(", ") + ">" 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?).
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'mrbgems/mruby-struct/src/struct.c', line 595 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_copy ⇒ Object
:nodoc:
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'mrbgems/mruby-struct/src/struct.c', line 366 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; } |
#inspect ⇒ Object 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)
72 73 74 75 76 77 78 |
# File 'mrbgems/mruby-struct/mrblib/struct.rb', line 72 def inspect begin self._inspect rescue SystemStackError "#<struct #{self.class.to_s}:...>" end end |
#length ⇒ Fixnum #size ⇒ Fixnum
Returns number of struct members.
631 632 633 634 635 |
# File 'mrbgems/mruby-struct/src/struct.c', line 631 static mrb_value mrb_struct_len(mrb_state *mrb, mrb_value self) { return mrb_fixnum_value(RSTRUCT_LEN(self)); } |
#members ⇒ Array
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]
111 112 113 114 115 |
# File 'mrbgems/mruby-struct/src/struct.c', line 111 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 |
#length ⇒ Fixnum #size ⇒ Fixnum
Returns number of struct members.
631 632 633 634 635 |
# File 'mrbgems/mruby-struct/src/struct.c', line 631 static mrb_value mrb_struct_len(mrb_state *mrb, mrb_value self) { return mrb_fixnum_value(RSTRUCT_LEN(self)); } |
#to_a ⇒ Array #values ⇒ Array
Create an array from struct values.
644 645 646 647 648 |
# File 'mrbgems/mruby-struct/src/struct.c', line 644 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_h ⇒ Hash
Create a hash from member names and struct values.
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'mrbgems/mruby-struct/src/struct.c', line 656 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_a ⇒ Array #values ⇒ Array
Create an array from struct values.
644 645 646 647 648 |
# File 'mrbgems/mruby-struct/src/struct.c', line 644 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_at ⇒ Object
672 673 674 675 676 677 678 679 680 681 |
# File 'mrbgems/mruby-struct/src/struct.c', line 672 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); } |