1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RubyにEnumerator#rest みたいなのが欲しい

Posted at

Ruby の Enumerator で、列挙はひとつずつしかできない。まとめていくつか列挙できるメソッドが欲しい。

こんな感じ。

Ruby
class Enumerator
  def rest(n=nil)
    ary = []
    case n
    in nil
      loop { ary << self.next }
    in Integer
      n.times { ary << self.next }
    end
    ary
  end
end


a = [*1..10].to_enum

a.next    #=>1
a.next    #=>2
a.rest(4) #=>[3, 4, 5, 6]
a.next    #=>7
a.rest    #=>[8, 9, 10]

ダメですかねえ…。

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?