112112# * #inspect, #to_s: Returns a string representation of `self`.
113113# * #to_h: Returns a hash of the member name/value pairs in `self`.
114114#
115- class Struct [Elem]
116- include Enumerable [Elem]
117-
115+ class Struct
118116 # The types that can be used when "indexing" into a `Struct` via `[]`, `[]=`, `dig`, and
119117 # `deconstruct_keys`.
120118 #
@@ -298,48 +296,6 @@ class Struct[Elem]
298296 #
299297 def == : (untyped other) -> bool
300298
301- # <!--
302- # rdoc-file=struct.c
303- # - eql?(other) -> true or false
304- # -->
305- # Returns `true` if and only if the following are true; otherwise returns
306- # `false`:
307- #
308- # * `other.class == self.class`.
309- # * For each member name `name`, `other.name.eql?(self.name)`.
310- #
311- # Customer = Struct.new(:name, :address, :zip)
312- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
313- # joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
314- # joe_jr.eql?(joe) # => true
315- # joe_jr[:name] = 'Joe Smith, Jr.'
316- # joe_jr.eql?(joe) # => false
317- #
318- #
319- # Related: Object#==.
320- #
321- def eql? : (untyped other) -> bool
322-
323- # <!--
324- # rdoc-file=struct.c
325- # - hash -> integer
326- # -->
327- # Returns the integer hash value for `self`.
328- #
329- # Two structs of the same class and with the same content will have the same
330- # hash code (and will compare using Struct#eql?):
331- #
332- # Customer = Struct.new(:name, :address, :zip)
333- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
334- # joe_jr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
335- # joe.hash == joe_jr.hash # => true
336- # joe_jr[:name] = 'Joe Smith, Jr.'
337- # joe.hash == joe_jr.hash # => false
338- #
339- # Related: Object#hash.
340- #
341- def hash : () -> Integer
342-
343299 # <!--
344300 # rdoc-file=struct.c
345301 # - inspect -> string
@@ -360,320 +316,4 @@ class Struct[Elem]
360316 # joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
361317 #
362318 alias to_s inspect
363-
364- # <!--
365- # rdoc-file=struct.c
366- # - to_a -> array
367- # -->
368- # Returns the values in `self` as an array:
369- #
370- # Customer = Struct.new(:name, :address, :zip)
371- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
372- # joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
373- #
374- # Related: #members.
375- #
376- def to_a : () -> Array[Elem]
377-
378- # <!--
379- # rdoc-file=struct.c
380- # - to_h -> hash
381- # - to_h {|name, value| ... } -> hash
382- # -->
383- # Returns a hash containing the name and value for each member:
384- #
385- # Customer = Struct.new(:name, :address, :zip)
386- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
387- # h = joe.to_h
388- # h # => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
389- #
390- # If a block is given, it is called with each name/value pair; the block should
391- # return a 2-element array whose elements will become a key/value pair in the
392- # returned hash:
393- #
394- # h = joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}
395- # h # => {:NAME=>"JOE SMITH", :ADDRESS=>"123 MAPLE, ANYTOWN NC", :ZIP=>"12345"}
396- #
397- # Raises ArgumentError if the block returns an inappropriate value.
398- #
399- def to_h : () -> Hash[Symbol, Elem]
400- | [K, V] () { (Symbol key, Elem value) -> [K, V] } -> Hash[K, V]
401-
402- # <!-- rdoc-file=struct.c -->
403- # Returns the values in `self` as an array:
404- #
405- # Customer = Struct.new(:name, :address, :zip)
406- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
407- # joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
408- #
409- # Related: #members.
410- #
411- alias values to_a
412-
413- # <!--
414- # rdoc-file=struct.c
415- # - size -> integer
416- # -->
417- # Returns the number of members.
418- #
419- # Customer = Struct.new(:name, :address, :zip)
420- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
421- # joe.size #=> 3
422- #
423- def size : () -> Integer
424-
425- # <!-- rdoc-file=struct.c -->
426- # Returns the number of members.
427- #
428- # Customer = Struct.new(:name, :address, :zip)
429- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
430- # joe.size #=> 3
431- #
432- alias length size
433-
434- # <!--
435- # rdoc-file=struct.c
436- # - each {|value| ... } -> self
437- # - each -> enumerator
438- # -->
439- # Calls the given block with the value of each member; returns `self`:
440- #
441- # Customer = Struct.new(:name, :address, :zip)
442- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
443- # joe.each {|value| p value }
444- #
445- # Output:
446- #
447- # "Joe Smith"
448- # "123 Maple, Anytown NC"
449- # 12345
450- #
451- # Returns an Enumerator if no block is given.
452- #
453- # Related: #each_pair.
454- #
455- def each : () -> Enumerator[Elem, self ]
456- | () { (Elem value) -> void } -> self
457-
458- # <!--
459- # rdoc-file=struct.c
460- # - each_pair {|(name, value)| ... } -> self
461- # - each_pair -> enumerator
462- # -->
463- # Calls the given block with each member name/value pair; returns `self`:
464- #
465- # Customer = Struct.new(:name, :address, :zip) # => Customer
466- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
467- # joe.each_pair {|(name, value)| p "#{name} => #{value}" }
468- #
469- # Output:
470- #
471- # "name => Joe Smith"
472- # "address => 123 Maple, Anytown NC"
473- # "zip => 12345"
474- #
475- # Returns an Enumerator if no block is given.
476- #
477- # Related: #each.
478- #
479- def each_pair : () -> Enumerator[[Symbol, Elem], self ]
480- | () { ([Symbol, Elem] key_value) -> void } -> self
481-
482- # <!--
483- # rdoc-file=struct.c
484- # - struct[name] -> object
485- # - struct[n] -> object
486- # -->
487- # Returns a value from `self`.
488- #
489- # With symbol or string argument `name` given, returns the value for the named
490- # member:
491- #
492- # Customer = Struct.new(:name, :address, :zip)
493- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
494- # joe[:zip] # => 12345
495- #
496- # Raises NameError if `name` is not the name of a member.
497- #
498- # With integer argument `n` given, returns `self.values[n]` if `n` is in range;
499- # see Array@Array+Indexes:
500- #
501- # joe[2] # => 12345
502- # joe[-2] # => "123 Maple, Anytown NC"
503- #
504- # Raises IndexError if `n` is out of range.
505- #
506- def [] : (index name_or_position) -> Elem
507-
508- # <!--
509- # rdoc-file=struct.c
510- # - struct[name] = value -> value
511- # - struct[n] = value -> value
512- # -->
513- # Assigns a value to a member.
514- #
515- # With symbol or string argument `name` given, assigns the given `value` to the
516- # named member; returns `value`:
517- #
518- # Customer = Struct.new(:name, :address, :zip)
519- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
520- # joe[:zip] = 54321 # => 54321
521- # joe # => #<struct Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=54321>
522- #
523- # Raises NameError if `name` is not the name of a member.
524- #
525- # With integer argument `n` given, assigns the given `value` to the `n`-th
526- # member if `n` is in range; see Array@Array+Indexes:
527- #
528- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
529- # joe[2] = 54321 # => 54321
530- # joe[-3] = 'Joseph Smith' # => "Joseph Smith"
531- # joe # => #<struct Customer name="Joseph Smith", address="123 Maple, Anytown NC", zip=54321>
532- #
533- # Raises IndexError if `n` is out of range.
534- #
535- def []= : (index name_or_position, Elem value) -> Elem
536-
537- # <!--
538- # rdoc-file=struct.c
539- # - select {|value| ... } -> array
540- # - select -> enumerator
541- # -->
542- # With a block given, returns an array of values from `self` for which the block
543- # returns a truthy value:
544- #
545- # Customer = Struct.new(:name, :address, :zip)
546- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
547- # a = joe.select {|value| value.is_a?(String) }
548- # a # => ["Joe Smith", "123 Maple, Anytown NC"]
549- # a = joe.select {|value| value.is_a?(Integer) }
550- # a # => [12345]
551- #
552- # With no block given, returns an Enumerator.
553- #
554- def select : () -> Enumerator[Elem, Array[Elem]]
555- | () { (Elem value) -> boolish } -> Array[Elem]
556-
557- # <!-- rdoc-file=struct.c -->
558- # With a block given, returns an array of values from `self` for which the block
559- # returns a truthy value:
560- #
561- # Customer = Struct.new(:name, :address, :zip)
562- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
563- # a = joe.select {|value| value.is_a?(String) }
564- # a # => ["Joe Smith", "123 Maple, Anytown NC"]
565- # a = joe.select {|value| value.is_a?(Integer) }
566- # a # => [12345]
567- #
568- # With no block given, returns an Enumerator.
569- #
570- alias filter select
571-
572- # <!--
573- # rdoc-file=struct.c
574- # - values_at(*integers) -> array
575- # - values_at(integer_range) -> array
576- # -->
577- # Returns an array of values from `self`.
578- #
579- # With integer arguments `integers` given, returns an array containing each
580- # value given by one of `integers`:
581- #
582- # Customer = Struct.new(:name, :address, :zip)
583- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
584- # joe.values_at(0, 2) # => ["Joe Smith", 12345]
585- # joe.values_at(2, 0) # => [12345, "Joe Smith"]
586- # joe.values_at(2, 1, 0) # => [12345, "123 Maple, Anytown NC", "Joe Smith"]
587- # joe.values_at(0, -3) # => ["Joe Smith", "Joe Smith"]
588- #
589- # Raises IndexError if any of `integers` is out of range; see
590- # Array@Array+Indexes.
591- #
592- # With integer range argument `integer_range` given, returns an array containing
593- # each value given by the elements of the range; fills with `nil` values for
594- # range elements larger than the structure:
595- #
596- # joe.values_at(0..2)
597- # # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
598- # joe.values_at(-3..-1)
599- # # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
600- # joe.values_at(1..4) # => ["123 Maple, Anytown NC", 12345, nil, nil]
601- #
602- # Raises RangeError if any element of the range is negative and out of range;
603- # see Array@Array+Indexes.
604- #
605- def values_at : (*int | range[int?] positions) -> Array[Elem]
606-
607- # <!--
608- # rdoc-file=struct.c
609- # - members -> array_of_symbols
610- # -->
611- # Returns the member names from `self` as an array:
612- #
613- # Customer = Struct.new(:name, :address, :zip)
614- # Customer.new.members # => [:name, :address, :zip]
615- #
616- # Related: #to_a.
617- #
618- def members : () -> Array[Symbol]
619-
620- # <!--
621- # rdoc-file=struct.c
622- # - dig(name, *identifiers) -> object
623- # - dig(n, *identifiers) -> object
624- # -->
625- # Finds and returns an object among nested objects. The nested objects may be
626- # instances of various classes. See [Dig Methods](rdoc-ref:dig_methods.rdoc).
627- #
628- # Given symbol or string argument `name`, returns the object that is specified
629- # by `name` and `identifiers`:
630- #
631- # Foo = Struct.new(:a)
632- # f = Foo.new(Foo.new({b: [1, 2, 3]}))
633- # f.dig(:a) # => #<struct Foo a={:b=>[1, 2, 3]}>
634- # f.dig(:a, :a) # => {:b=>[1, 2, 3]}
635- # f.dig(:a, :a, :b) # => [1, 2, 3]
636- # f.dig(:a, :a, :b, 0) # => 1
637- # f.dig(:b, 0) # => nil
638- #
639- # Given integer argument `n`, returns the object that is specified by `n` and
640- # `identifiers`:
641- #
642- # f.dig(0) # => #<struct Foo a={:b=>[1, 2, 3]}>
643- # f.dig(0, 0) # => {:b=>[1, 2, 3]}
644- # f.dig(0, 0, :b) # => [1, 2, 3]
645- # f.dig(0, 0, :b, 0) # => 1
646- # f.dig(:b, 0) # => nil
647- #
648- def dig : (index name_or_position) -> Elem
649- | (index name_or_position, untyped , *untyped ) -> untyped
650-
651- # <!-- rdoc-file=struct.c -->
652- # Returns the values in `self` as an array:
653- #
654- # Customer = Struct.new(:name, :address, :zip)
655- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
656- # joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
657- #
658- # Related: #members.
659- #
660- alias deconstruct to_a
661-
662- # <!--
663- # rdoc-file=struct.c
664- # - deconstruct_keys(array_of_names) -> hash
665- # -->
666- # Returns a hash of the name/value pairs for the given member names.
667- #
668- # Customer = Struct.new(:name, :address, :zip)
669- # joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
670- # h = joe.deconstruct_keys([:zip, :address])
671- # h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
672- #
673- # Returns all names and values if `array_of_names` is `nil`:
674- #
675- # h = joe.deconstruct_keys(nil)
676- # h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
677- #
678- def deconstruct_keys : (Array[index & Hash::_Key]? indices) -> Hash[index & Hash::_Key, Elem]
679319end
0 commit comments