diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-06-21 00:11:35 -0700 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-06-21 00:11:35 -0700 |
| commit | d105ea496f686b6ef3071e127864fab4677befac (patch) | |
| tree | 792c7429c9ab5f93171ebe7f5f852d3157fdccd7 /mrblib | |
| parent | 0f2b69642d589aa5a55de7b4807d9b3072e4cc52 (diff) | |
| parent | 4780b653a47c1bacd2412130d77cbe5b3f0b30d3 (diff) | |
| download | mruby-d105ea496f686b6ef3071e127864fab4677befac.tar.gz mruby-d105ea496f686b6ef3071e127864fab4677befac.zip | |
Merge pull request #305 from bovi/optional-feature
Tests are executed based on available features
Diffstat (limited to 'mrblib')
| -rw-r--r-- | mrblib/struct.rb | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/mrblib/struct.rb b/mrblib/struct.rb index 4b6d767a9..5d0ede90f 100644 --- a/mrblib/struct.rb +++ b/mrblib/struct.rb @@ -2,45 +2,49 @@ # Struct # # ISO 15.2.18 -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 +if Object.const_defined?(:Struct) + class Struct - ## - # 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 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 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 + ## + # 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 + |
