Skip to content

Instantly share code, notes, and snippets.

@dlecocq
Created January 21, 2014 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dlecocq/8549841 to your computer and use it in GitHub Desktop.
Save dlecocq/8549841 to your computer and use it in GitHub Desktop.
This behave differently in jruby and MRI
#! /usr/bin/env ruby
@queues = 10.times.map { Array.new }
# An enumerator that yields `nil` if none of the queues has any items, but
# otherwise round-robins across them.
@foo = Enumerator.new do |enum|
loop do
found = false
@queues.each do |queue|
job = queue.pop
unless job.nil?
found = true
enum.yield(job)
end
end
enum.yield(nil) unless found
end
end
# Pop up to 'count' objects from
def pop(count)
jobs = []
while jobs.length < count
job = @foo.next
break if job.nil?
jobs.push(job)
end
jobs
end
# First, verify that the there are no items available
puts "Pop before put: #{pop(5)}"
@queues[5].push(1)
# Now, I'd expect the item to be immediately available after putting it, but in
# jRuby, it's not yielded until the second pop.
puts "First pop after put: #{pop(5)}"
puts "Second pop after put: #{pop(5)}"
# jruby 1.7.9 (1.9.3p392) 2013-12-06 87b108a on Java HotSpot(TM) 64-Bit Server VM 1.7.0_45-b18 [darwin-x86_64]
# Pop before put: []
# First pop after put: []
# Second pop after put: [1]
# ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]
# Pop before put: []
# First pop after put: [1]
# Second pop after put: []
# ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin13.0.0]
# Pop before put: []
# First pop after put: [1]
# Second pop after put: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment