Feature #8572
closedFiber should be a Enumerable
Description
I'm thinking that Fiber should be a Enumerable. it's easy and reasonable to implement.
Updated by matz (Yukihiro Matsumoto) over 11 years ago
I think this is a good idea, and since it's a mere addition, we don't have to worry about compatibility.
Matz.
Updated by mattn (Yasuhiro Matsumoto) over 11 years ago
ko1 (Koichi Sasada) wrote:
Use case?
f = Fiber.new {
Fiber.yield 3
Fiber.yield 2
Fiber.yield 1
Fiber.yield 0
}
f.each do |x|
puts x
end
For example, I want to do each
for Fiber. But it can't
Updated by zzak (zzak _) over 11 years ago
You could always included Enumerable
in your class that implements Fiber
and define #each
Updated by zzak (zzak _) over 11 years ago
Sorry, I missed your gist orz
Updated by knu (Akinori MUSHA) over 11 years ago
We have Generator in 1.8 and Enumerator in 1.9+ that work exactly the
same as the given example.
# In ruby 1.8, require 'generator' and call Generator.new instead
enum = Enumerator.new { |g|
g.yield 3
g.yield 2
g.yield 1
g.yield 0
}
# internal style
enum.each do |x|
puts x
end
# external style
while enum.next?
puts enum.next
end
One of the reasons why Fiber
was originally introduced was to
reimplement Generator
of ruby 1.8 in better and faster way. Using
Fiber
as implementation technique, Generator
was successfully
integrated into Enumerator
in Ruby 1.9.
So, Fiber
is there as a low-level API for implementing Enumerator
in
the first place. It does not make much sense to me to add a
non-primitive feature to Fiber
just to make it work like Enumerator
.
I think we need a real use case that would explain why Fiber
should be
used that way.
Updated by mattn (Yasuhiro Matsumoto) over 11 years ago
I think most of use case of using Fiber
is something like getting stream data or catching-up draining entities. Maybe we won't use Fiber
for getting limited resources. For example, If Fiber
has each
, I guess that we will be possible to write twitter client elegant. :)
f = Fiber.new {
# getting twitter status
loop {
Fiber.yield tweet_status
}
}
f.each do |x|
puts "#{x.screen_name}: #{x.text}"
end
Updated by matz (Yukihiro Matsumoto) over 11 years ago
+1
Matz.
Updated by ko1 (Koichi Sasada) over 11 years ago
Why you don't use Enumerator?
class TS
attr_accessor :screen_name, :text
def initialize
@screen_name = "foo"
@text = "sample"
end
end
f = Enumerator.new {|g|
# getting twitter status
loop {
g.yield TS.new
}
}
f.each do |x|
puts "#{x.screen_name}: #{x.text}"
end
// SASADA Koichi at atdot dot net
Updated by mattn (Yasuhiro Matsumoto) over 11 years ago
Why you don't use Enumerator?
Ah, make sense.
Updated by ko1 (Koichi Sasada) about 11 years ago
- Category set to core
- Status changed from Open to Feedback
- Target version set to 2.6
Can I close it?
Updated by mattn (Yasuhiro Matsumoto) almost 11 years ago
Koichi Sasada wrote:
Can I close it?
Sorry for delay. Close this please.
Updated by nobu (Nobuyoshi Nakada) almost 11 years ago
- Status changed from Feedback to Closed