How to know whether current Fiber is the root Fiber?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, calling Fiber.yield without being in the context of a created
Fiber raises an error:

can't yield from root fiber (FiberError)

It makes sense, of course. However I would like to know how to check
whether I'm into the root Fiber or not. The documentation doesn't
provide a method for this purpose. Is there a way? I expected
something like Fiber#root?.

Thanks a lot.


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
7

7stud --

How about catching the exception?

begin
Fiber.yield
rescue FiberError
puts "In root fiber..."
puts "... so I am going to do something different here."
end

puts 'executing rest of program'

--output:--
In root fiber...
... so I am going to do something different here.
executing rest of program


Or, maybe do something like this:
 
7

7stud --

7stud -- wrote in post #991816:
For example:

require 'fiber'

root_fiber = Fiber.current

f = Fiber.new do
if Fiber.current.eql?(f)
puts 'not root fiber'
else
puts 'root fiber'
end

Fiber.yield "hello world"
end

f.resume

--output:--
not root fiber
root fiber

Instead, make that:

require 'fiber'

root_fiber = Fiber.current

f = Fiber.new do
if Fiber.current.eql?(root_fiber)
puts 'root fiber'
else
puts 'not root fiber'
end

Fiber.yield "hello world"
end

f.resume

if Fiber.current.eql?(root_fiber)
puts 'root fiber'
else
puts 'not root fiber'
end
 
I

Iñaki Baz Castillo

2011/4/8 7stud -- said:
Instead, make that:

require 'fiber'

root_fiber =3D Fiber.current

f =3D Fiber.new do
=C2=A0if Fiber.current.eql?(root_fiber)
=C2=A0 =C2=A0puts 'root fiber'
=C2=A0else
=C2=A0 =C2=A0puts 'not root fiber'
=C2=A0end

=C2=A0Fiber.yield "hello world"
end

f.resume

if Fiber.current.eql?(root_fiber)
=C2=A0puts 'root fiber'
else
=C2=A0puts 'not root fiber'
end

Great solution :)

Thanks a lot.

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top