Class: AwesomeExplain::Queue::SimpleQueue

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/awesome_explain/queue/simple_queue.rb

Instance Method Summary collapse

Constructor Details

#initializeSimpleQueue

Returns a new instance of SimpleQueue.



42
43
44
45
46
47
48
49
50
# File 'lib/awesome_explain/queue/simple_queue.rb', line 42

def initialize
  @queue = Queue.new
  # Thread.new do
  #   puts 'while true ==============================='
  #   command = @queue.pop(false)
  #   # command.run if command
  # end
  # @read_io, @write_io = IO.pipe
end

Instance Method Details

#<<(o) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/awesome_explain/queue/simple_queue.rb', line 52

def <<(o)
  # pop(false) until @queue.size < 2
  if @queue.size >= 2
    items = []
    while @queue.size >= 2
      items << @queue.pop
    end
    Thread.new { sleep 2; puts "Poped #{items}"; puts items.inspect }
  end
  puts "Adding to queue @@@@@@@@@@@@@"
  @queue << o
  # @write_io << '.'
  self
end

#pop(nonblock = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/awesome_explain/queue/simple_queue.rb', line 67

def pop(nonblock=false)
  # return unless @queue.size >= 5
  # @queue.size.times.each do

  # end
  puts Thread.current.inspect
  o = @queue.pop(nonblock)
  # @read_io.read(1)
  puts "<------ Element poped ------>"
  puts o
  o
end

#sizeObject



80
81
82
# File 'lib/awesome_explain/queue/simple_queue.rb', line 80

def size
  @queue.size
end

#to_ioObject



84
85
86
# File 'lib/awesome_explain/queue/simple_queue.rb', line 84

def to_io
  @read_io
end