Skip to content

Commit

Permalink
Like a Enumerable#take
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Nov 25, 2016
1 parent 743c1e7 commit c28a963
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mrbgems/mruby-enum-ext/mrblib/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,17 @@ def first(n=NONE)
end
return nil
else
a = []
i = 0
raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
i = n.to_int
raise ArgumentError, "attempt to take negative size" if i < 0
ary = []
return ary if i == 0
self.each do |*val|
break if n<=i
a.push val.__svalue
i += 1
ary << val.__svalue
i -= 1
break if i == 0
end
a
ary
end
end

Expand Down

0 comments on commit c28a963

Please sign in to comment.