• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Unexpected Result When Using Fibers

Resolved: Unexpected Result When Using Fibers

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I wrote a simple ruby fiber program to see how they work and got an unexpected result.
datafile
And the output from the above program
I’m not sure why its displaying #File:0x0000557ce26ce3c8 in the output.
Note: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]

Answer:

From the docs,

Upon yielding or termination the Fiber returns the value of the last executed expression


In addition to Fiber.yield calls, a fiber (like an ordinary function) returns the result of the final expression in the fiber.
The body of your fiber is this.
Inside the .each, you yield each line of the file, which gets printed out as you’ve already observed. But then, when the fiber is done, it yields a final value, which is the result of File.open. And File.open returns the File object itself. So your sg.resume actually sees six results, not five.
This actually points out a small issue in your program to begin with: You never close the file. You can do that either with File#close or by passing a block to File::open. In order to be completely safe, your fiber code should probably look like this.
Now, in addition to dealing with that pesky file handle, we have a way to detect when the fiber is done and we no longer get the “attempt to resume a terminated fiber” error.

If you have better answer, please add a comment about this, thank you!

ruby
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Why when i change manualy the url on my react page it ignores the .css file?

24/03/2023

Resolved: navigate and redirect not redirecting to path in react router

24/03/2023

Resolved: Time Complexity: Find the number of people who know a secret leetcode 2327

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.