diff options
| author | Yukihiro Matz Matsumoto <[email protected]> | 2013-03-01 21:38:02 +0900 |
|---|---|---|
| committer | Yukihiro Matz Matsumoto <[email protected]> | 2013-03-01 21:38:02 +0900 |
| commit | 8d2880bbfe229b7c8df327be2d046820cce07838 (patch) | |
| tree | e0a3d61fd34cfd01fd6db13468d10be5dd9dcfc9 /mrbgems/mruby-struct/mrblib/struct.rb | |
| parent | c0e1fc935c301dd86705d754bcf8493c5200eaf9 (diff) | |
| parent | 99a6de063cc4c4964074b27ad21553043b468368 (diff) | |
| download | mruby-8d2880bbfe229b7c8df327be2d046820cce07838.tar.gz mruby-8d2880bbfe229b7c8df327be2d046820cce07838.zip | |
Merge branch 'pluggable_struct' of https://github.com/mattn/mruby into mattn-pluggable_struct
Diffstat (limited to 'mrbgems/mruby-struct/mrblib/struct.rb')
| -rw-r--r-- | mrbgems/mruby-struct/mrblib/struct.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/mrbgems/mruby-struct/mrblib/struct.rb b/mrbgems/mruby-struct/mrblib/struct.rb new file mode 100644 index 000000000..5d0ede90f --- /dev/null +++ b/mrbgems/mruby-struct/mrblib/struct.rb @@ -0,0 +1,50 @@ +## +# Struct +# +# ISO 15.2.18 + +if Object.const_defined?(:Struct) + class Struct + + ## + # Calls the given block for each element of +self+ + # and pass the respective element. + # + # ISO 15.2.18.4.4 + def each(&block) + self.class.members.each{|field| + block.call(self[field]) + } + self + end + + ## + # Calls the given block for each element of +self+ + # and pass the name and value of the respectiev + # element. + # + # ISO 15.2.18.4.5 + def each_pair(&block) + self.class.members.each{|field| + block.call(field.to_sym, self[field]) + } + self + end + + ## + # Calls the given block for each element of +self+ + # and returns an array with all elements of which + # block is not false. + # + # ISO 15.2.18.4.7 + def select(&block) + ary = [] + self.class.members.each{|field| + val = self[field] + ary.push(val) if block.call(val) + } + ary + end + end +end + |
