A
A
Alexander Grishin2016-11-25 10:05:52
Ruby on Rails
Alexander Grishin, 2016-11-25 10:05:52

Why does Ruby need fiber?

Actually the question is in the header. Why is fiber needed in Ruby?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Grishin, 2016-11-30
@beerdy

In general, I myself figured out why Fibers are needed - in RUSSIAN speaking, most likely as external iterators, for example like this:

class PhotoClass
  def initialize(media)
    @fibs  = []
    @media = media
    @fib   = fib(media)
  end

  def read
    @fib.resume @media
  end

private
  def fib(media)
    Fiber.new do |media|
      loop do
        stop = true
        (0..media.length).each do |index|
          result = fiber_inner(index).resume media[index].stock_photos if media[index]

          unless result.nil?
            stop = false
            Fiber.yield ({ :index => index, :title => { :group => media[index].title, :image => media[index].stock_photos[result].title }, :url => media[index].stock_photos[result].image.url })
          end
        end
        Fiber.yield nil if stop
      end
    end
  end

protected
  def fiber_inner(num)
    return @fibs[num] if @fibs[num]

    @fibs[num] = Fiber.new do |content|
      content.each_with_index do |(image),index|
        Fiber.yield index
      end
      loop do Fiber.yield nil end
    end
  end
end

We use in the view:
<% loop do %>

                  <% image = @photo.read %>
                  <% break if image.nil? %>

                  <div class="portfolio-box cm<%= image[:index] %>-design">
                    <div class="portfolio-box-container work">
                      <img src="<%= image[:url] %>" alt="" data-at2x="<%= image[:url] %>">
                      <div class="portfolio-box-text">
                        <h3><%= image[:title][:group] %></h3>
                        <p>Краткое описание картинки изобра.</p>
                      </div>
                        <div class="work-bottom">
                          <a class="big-link-2 view-work" href="<%= image[:url]%>"><span class="icon_search"></span></a>
                          <a class="big-link-2" href="/complectation/index"><span class="icon_link"></span></a>
                        </div>

                    </div>
                  </div>
                  <% end %>

S
Sergey, 2016-11-25
Protko @Fesor

then why microflows are needed in erlang and goroutines in golang.

A
Andrey Demidenko, 2016-11-25
@Dem1

The main difference between Fiber and Thread is that you program the context switch yourself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question