Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-struct / mrblib / struct.rb
1 ##
2 # Struct
3 #
4 # ISO 15.2.18
5
6 if Object.const_defined?(:Struct)
7   class Struct
8
9     ##
10     # Calls the given block for each element of +self+
11     # and pass the respective element.
12     #
13     # ISO 15.2.18.4.4
14     def each(&block)
15       self.class.members.each{|field|
16         block.call(self[field])
17       }
18       self
19     end
20
21     ##
22     # Calls the given block for each element of +self+
23     # and pass the name and value of the respectiev
24     # element.
25     #
26     # ISO 15.2.18.4.5
27     def each_pair(&block)
28       self.class.members.each{|field|
29         block.call(field.to_sym, self[field])
30       }
31       self
32     end
33
34     ##
35     # Calls the given block for each element of +self+
36     # and returns an array with all elements of which
37     # block is not false.
38     #
39     # ISO 15.2.18.4.7
40     def select(&block)
41       ary = []
42       self.class.members.each{|field|
43         val = self[field]
44         ary.push(val) if block.call(val)
45       }
46       ary
47     end
48
49     def _inspect
50       name = self.class.to_s
51       if name[0] == "#"
52         str = "#<struct "
53       else
54         str = "#<struct #{name} "
55       end
56       buf = []
57       self.each_pair do |k,v|
58         buf.push [k.to_s + "=" + v._inspect]
59       end
60       str + buf.join(", ") + ">"
61     end
62
63     ##
64     # call-seq:
65     #   struct.to_s      -> string
66     #   struct.inspect   -> string
67     #
68     # Describe the contents of this struct in a string.
69     #
70     # 15.2.18.4.10(x)
71     #
72     def inspect
73       begin
74         self._inspect
75       rescue SystemStackError
76         "#<struct #{self.class.to_s}:...>"
77       end
78     end
79
80     ##
81     # 15.2.18.4.11(x)
82     #
83     alias to_s inspect
84
85     ##
86     # call-seq:
87     #   hsh.dig(key,...)                 -> object
88     #
89     # Extracts the nested value specified by the sequence of <i>key</i>
90     # objects by calling +dig+ at each step, returning +nil+ if any
91     # intermediate step is +nil+.
92     #
93     def dig(idx,*args)
94       n = self[idx]
95       if args.size > 0
96         n&.dig(*args)
97       else
98         n
99       end
100     end
101   end
102 end