diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-03-25 12:29:29 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-03-25 12:29:29 +0900 |
| commit | 896dc002ca2720dc7c8e60a0d54b2353169b2e04 (patch) | |
| tree | 9ef2114b93335e3fa0e8efb7ab1041c7f1b6ef10 /mrbgems/mruby-enum-ext | |
| parent | 1e73181da93f0399d914121bbd4fbec4588e9ddd (diff) | |
| download | mruby-896dc002ca2720dc7c8e60a0d54b2353169b2e04.tar.gz mruby-896dc002ca2720dc7c8e60a0d54b2353169b2e04.zip | |
use to_int to integer duck-type check
Diffstat (limited to 'mrbgems/mruby-enum-ext')
| -rw-r--r-- | mrbgems/mruby-enum-ext/mrblib/enum.rb | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index 50eaa6a4d..bca69e08f 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -13,9 +13,10 @@ module Enumerable # a.drop(3) #=> [4, 5, 0] def drop(n) - raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer + raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) raise ArgumentError, "attempt to drop negative size" if n < 0 + n = n.to_int ary = [] self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 } ary @@ -51,9 +52,10 @@ module Enumerable # a.take(3) #=> [1, 2, 3] def take(n) - raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer + raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) raise ArgumentError, "attempt to take negative size" if n < 0 + n = n.to_int ary = [] self.each do |*val| break if ary.size >= n @@ -102,10 +104,11 @@ module Enumerable # [8, 9, 10] def each_cons(n, &block) - raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer + raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) raise ArgumentError, "invalid size" if n <= 0 ary = [] + n = n.to_int self.each do |*val| ary.shift if ary.size == n ary << val.__svalue @@ -128,10 +131,11 @@ module Enumerable # [10] def each_slice(n, &block) - raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer + raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) raise ArgumentError, "invalid slice size" if n <= 0 ary = [] + n = n.to_int self.each do |*val| ary << val.__svalue if ary.size == n @@ -560,10 +564,9 @@ module Enumerable end end else - unless n.kind_of? Integer - raise TypeError, "expected Integer for 1st argument" - end + raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) + n = n.to_int self.each do|*val| ary.push val end |
