diff options
| author | Jun Hiroe <[email protected]> | 2014-03-18 22:48:57 +0900 |
|---|---|---|
| committer | Jun Hiroe <[email protected]> | 2014-03-18 23:06:01 +0900 |
| commit | 02ac5dd9f1bdf0b2c99a9a86d3f1a029ad09a1c0 (patch) | |
| tree | bbb5edb7501a2fce97c073137f3cb8418feca0a3 /mrbgems/mruby-enum-ext | |
| parent | 31b2980acb7218b0cc13cbc658208cde641a7ff8 (diff) | |
| download | mruby-02ac5dd9f1bdf0b2c99a9a86d3f1a029ad09a1c0.tar.gz mruby-02ac5dd9f1bdf0b2c99a9a86d3f1a029ad09a1c0.zip | |
Add Enumerable#count
Diffstat (limited to 'mrbgems/mruby-enum-ext')
| -rw-r--r-- | mrbgems/mruby-enum-ext/mrblib/enum.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index 90f321596..1ca62b673 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -208,4 +208,22 @@ module Enumerable a end end + + def count(v=nil, &block) + count = 0 + if block + self.each do |e| + count += 1 if block.call(e) + end + else + if v == nil + self.each { count += 1 } + else + self.each do |e| + count += 1 if e == v + end + end + end + count + end end |
