Last change
on this file since 446 was 439, checked in by coas-nagasima, 4 years ago |
mrubyを2.1.1に更新
|
-
Property svn:eol-style
set to
native
-
Property svn:mime-type
set to
text/x-ruby;charset=UTF-8
|
File size:
2.0 KB
|
Line | |
---|
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(recur_list)
|
---|
50 | return "#<struct #{self.class}:...>" if recur_list[self.object_id]
|
---|
51 | recur_list[self.object_id] = true
|
---|
52 | name = self.class.to_s
|
---|
53 | if name[0] == "#"
|
---|
54 | str = "#<struct "
|
---|
55 | else
|
---|
56 | str = "#<struct #{name} "
|
---|
57 | end
|
---|
58 | buf = []
|
---|
59 | self.each_pair do |k,v|
|
---|
60 | buf.push k.to_s + "=" + v._inspect(recur_list)
|
---|
61 | end
|
---|
62 | str + buf.join(", ") + ">"
|
---|
63 | end
|
---|
64 |
|
---|
65 | ##
|
---|
66 | # call-seq:
|
---|
67 | # struct.to_s -> string
|
---|
68 | # struct.inspect -> string
|
---|
69 | #
|
---|
70 | # Describe the contents of this struct in a string.
|
---|
71 | #
|
---|
72 | # 15.2.18.4.10(x)
|
---|
73 | #
|
---|
74 | def inspect
|
---|
75 | self._inspect({})
|
---|
76 | end
|
---|
77 |
|
---|
78 | ##
|
---|
79 | # 15.2.18.4.11(x)
|
---|
80 | #
|
---|
81 | alias to_s inspect
|
---|
82 |
|
---|
83 | ##
|
---|
84 | # call-seq:
|
---|
85 | # hsh.dig(key,...) -> object
|
---|
86 | #
|
---|
87 | # Extracts the nested value specified by the sequence of <i>key</i>
|
---|
88 | # objects by calling +dig+ at each step, returning +nil+ if any
|
---|
89 | # intermediate step is +nil+.
|
---|
90 | #
|
---|
91 | def dig(idx,*args)
|
---|
92 | n = self[idx]
|
---|
93 | if args.size > 0
|
---|
94 | n&.dig(*args)
|
---|
95 | else
|
---|
96 | n
|
---|
97 | end
|
---|
98 | end
|
---|
99 | end
|
---|
100 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.