Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-struct/mrblib/struct.rb

raw
1 ##
2 # Struct
3 #
4 # ISO 15.2.18
5 class Struct
6 include Enumerable
7
8 ##
9 # Calls the given block for each element of +self+
10 # and pass the respective element.
11 #
12 # ISO 15.2.18.4.4
13 def each(&block)
14 self.class.members.each{|field|
15 block.call(self[field])
16 }
17 self
18 end
19
20 ##
21 # Calls the given block for each element of +self+
22 # and pass the name and value of the respective
23 # element.
24 #
25 # ISO 15.2.18.4.5
26 def each_pair(&block)
27 self.class.members.each{|field|
28 block.call(field.to_sym, self[field])
29 }
30 self
31 end
32
33 ##
34 # Calls the given block for each element of +self+
35 # and returns an array with all elements of which
36 # block is not false.
37 #
38 # ISO 15.2.18.4.7
39 def select(&block)
40 ary = []
41 self.class.members.each{|field|
42 val = self[field]
43 ary.push(val) if block.call(val)
44 }
45 ary
46 end
47
48 ##
49 # 15.2.18.4.11(x)
50 #
51 alias to_s inspect
52
53 ##
54 # call-seq:
55 # hsh.dig(key,...) -> object
56 #
57 # Extracts the nested value specified by the sequence of <i>key</i>
58 # objects by calling +dig+ at each step, returning +nil+ if any
59 # intermediate step is +nil+.
60 #
61 def dig(idx,*args)
62 n = self[idx]
63 if args.size > 0
64 n&.dig(*args)
65 else
66 n
67 end
68 end
69 end