6

I use ruby1.9.2p180 (2011-02-18 revision 30909) i686-linux. Fiber#alive? returns an undefined error:

fiber = Fiber.new{puts 'hello'}
fiber.alive?
=> undefined error

Other methods, for example, Fiber#resume , Fiber.yield seem to be working fine. What is wrong?

By the way, what is the difference between Fiber#resume and Fiber#transfer?

sawa
  • 165,429
  • 45
  • 277
  • 381

1 Answers1

4

You forgot to require 'fiber' first:

f = Fiber.new { puts 'hello' }
  => #<Fiber:0x896109c> 

f.alive?
  # NoMethodError: undefined method `alive?' for #<Fiber:0x896109c>
  #      from (irb):2
  #      from /home/johnf/.rvm/rubies/ruby-1.9.2-rc1/bin/irb:17:in `<main>'

require 'fiber'
  => true 

f.alive?
  => true 
John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 2
    Thanks. I didn't forget it, I didn't know it. It didn't say so in the doc. Its says you need to do that for `Fiber.current`, but it doesn't say so for `Fiber#alive?`. – sawa Mar 16 '11 at 16:55
  • Probably a documentation oversight, then. When in doubt, read the code. :) – John Feminella Mar 16 '11 at 16:57
  • 2
    So requiring fiber is only required for some, not all of Fiber's methods? – Andrew Grimm Mar 16 '11 at 22:40
  • 1
    @Andrew That seems to be the case. As far as I checked on my irb, `.new`, `.yield`, `#resume` don't require `require`. `.current`, `#alive?`, `#transfer` do. – sawa Mar 17 '11 at 01:45