LoginSignup
0
0

More than 1 year has passed since last update.

【Ruby】with_object

Posted at

with_object

繰り返しの各要素にオブジェクトを添えてブロックを繰り返し、オブジェクトを返り値として返す。

# 0,1,2 と呼びだす enumeratorを作る
to_three = Enumerator.new do |y|
  3.times do |x|
    y << x
  end
end

to_three_with_string = to_three.with_object("foo")
to_three_with_string.each do |x,string|
  puts "#{string}: #{x}"
end
# => foo:0
# => foo:1
# => foo:2

参考

0
0
0

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
0
0