Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to know whether current Fiber is the root Fiber?

Reply
Thread Tools

How to know whether current Fiber is the root Fiber?

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-08-2011
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
<(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      04-08-2011
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:

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      04-08-2011
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-11-2011
2011/4/8 7stud -- <(E-Mail Removed)>:
> 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
<(E-Mail Removed)>

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      04-11-2011
On Mon, Apr 11, 2011 at 9:30 AM, I=F1aki Baz Castillo <(E-Mail Removed)> wrote=
:
> 2011/4/8 7stud -- <(E-Mail Removed)>:
>> Instead, make that:
>>
>> require 'fiber'
>>
>> root_fiber =3D Fiber.current
>>
>> f =3D Fiber.new do
>> =A0if Fiber.current.eql?(root_fiber)
>> =A0 =A0puts 'root fiber'
>> =A0else
>> =A0 =A0puts 'not root fiber'
>> =A0end
>>
>> =A0Fiber.yield "hello world"
>> end
>>
>> f.resume
>>
>> if Fiber.current.eql?(root_fiber)
>> =A0puts 'root fiber'
>> else
>> =A0puts 'not root fiber'
>> end

>
> Great solution


You could even use #equal? instead of #eql? just in case #eql? is overridde=
n.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments