# Ruby 1.9 - equivalent of Fiber.current?

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-equivalent-of-fiber-current/130708>\
**Category:** Ruby\
**Created:** [March 2, 2008, 8:27am UTC](https://www.ruby-forum.com/t/ruby-1-9-equivalent-of-fiber-current/130708 "2008-03-02T08:27:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [March 2, 2008, 8:27am UTC](https://www.ruby-forum.com/t/ruby-1-9-equivalent-of-fiber-current/130708/1 "2008-03-02T08:27:28Z")

</div>

Hello,

Is it possible to detect whether a method is being called by a Fiber in  
Ruby 1.9?

I hoped that there would be a Fiber.current method (just like  
Thread.current), but that does not exist:

> > Fiber.methods(false).sort  
> > =\> [:new, :yield]

I need to know this information for a method that’s part of an  
application-level thread/fiber/task scheduler for my Ruby-VPI  
project[1]. This method must either (1) run Fiber.yield (if called by a  
Fiber) or (2) relay control from the Ruby interpreter to a C extension.

Thanks for your consideration.

[1] [http://ruby-vpi.rubyforge.org](http://ruby-vpi.rubyforge.org)

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [March 2, 2008, 8:34am UTC](https://www.ruby-forum.com/t/ruby-1-9-equivalent-of-fiber-current/130708/2 "2008-03-02T08:34:17Z")

</div>

Suraj K. wrote:

> Is it possible to detect whether a method  
> is being called by a Fiber in Ruby 1.9?

Heh, I should have just tried it:

> > Fiber.yield  
> > FiberError: can’t yield from root fiber

So the solution is:

begin

# inside a fiber

Fiber.yield  
rescue FiberError

# not inside a fiber…

end

But I suppose Fiber.yield could raise FiberError for other reasons…  
is there a better way to do this detection?

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [March 2, 2008, 10:42am UTC](https://www.ruby-forum.com/t/ruby-1-9-equivalent-of-fiber-current/130708/3 "2008-03-02T10:42:37Z")

</div>

> > > > > “S” == Suraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > > Fiber.methods(false).sort

vgs% ruby -rfiber -e ‘p Fiber.methods(false)’  
[:new, :yield, :current]  
vgs%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [March 2, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/ruby-1-9-equivalent-of-fiber-current/130708/4 "2008-03-02T18:05:34Z")

</div>

ts wrote:

> vgs% ruby -rfiber -e ‘p Fiber.methods(false)’  
> [:new, :yield, :current]  
> vgs%

Aha! We need to require ‘fiber’ in order  
to get the Fiber.current method. Thanks!
