Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Enumerable | cycle

概要

Enumerable#cycle

詳細

Enumerable#cycle は指定回数各要素を繰り返します。
引数を指定しない場合は永久に繰り返します。

サンプル

コード
# encoding: utf-8
require "pp"

list = %w{hoge hige hage}

enum = list.cycle(2)
p enum.to_a.join("|")

list.cycle(2) {|e|print "#{e}|"}
出力
"hoge|hige|hage|hoge|hige|hage"
hoge|hige|hage|hoge|hige|hage|