source: EcnlProtoTool/trunk/mruby-1.2.0/mrbgems/mruby-struct/mrblib/struct.rb@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 1.6 KB
Line 
1##
2# Struct
3#
4# ISO 15.2.18
5
6if 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 end
85end
86
Note: See TracBrowser for help on using the repository browser.