Skip to content

Instantly share code, notes, and snippets.

@snarlysodboxer
Created April 30, 2013 21:50
Show Gist options
  • Save snarlysodboxer/5492217 to your computer and use it in GitHub Desktop.
Save snarlysodboxer/5492217 to your computer and use it in GitHub Desktop.

This script shows ruby 1.8.7's strange behavior relating to the order of a returned array after setting certain unrelated hashes.

a = { :a => nil } running this code first causes the below to return in reverse order. b = { :a => nil } running this code first causes the below to return in correct order. { :y => true, :s => true }.collect { |k, v| k } is the code that returns the array. It is run the same way every iteration.

For this bug to manifest, irb must be restarted between tests. Once an irb session has returned the array in a particular direction, it will continue to return it in that direction no matter what is done until you exit and restart irb. (Thus, this bash script to automate starting and using irb.) This bug does not happen in ruby 1.9.3, meaning no matter what you do, you will always get the array returned in the same direction. The results are repeatably reliable.

The test_ruby_bug.txt file is the output of this script.

#!/bin/bash
cat "" > test_ruby_bug.txt
for letter in {a..z}
do
irb <<EOF
a = { :$letter => nil }
open('test_ruby_bug.txt', 'a') { |f|
f.print("a = { :$letter => nil } ; (code) => #{{ :y => nil, :s => nil }.collect { |k, v|
k
}.inspect} ")
}
EOF
irb <<EOF
$letter = { :a => nil }
open('test_ruby_bug.txt', 'a') { |f|
f.puts("$letter = { :a => nil } ; (code) => #{{ :y => nil, :s => nil }.collect { |k, v|
k
}.inspect}")
}
EOF
done
a = { :a => nil } ; (code) => [:s, :y] a = { :a => nil } ; (code) => [:s, :y]
a = { :b => nil } ; (code) => [:s, :y] b = { :a => nil } ; (code) => [:s, :y]
a = { :c => nil } ; (code) => [:s, :y] c = { :a => nil } ; (code) => [:s, :y]
a = { :d => nil } ; (code) => [:y, :s] d = { :a => nil } ; (code) => [:y, :s]
a = { :e => nil } ; (code) => [:s, :y] e = { :a => nil } ; (code) => [:s, :y]
a = { :f => nil } ; (code) => [:s, :y] f = { :a => nil } ; (code) => [:s, :y]
a = { :g => nil } ; (code) => [:y, :s] g = { :a => nil } ; (code) => [:y, :s]
a = { :h => nil } ; (code) => [:y, :s] h = { :a => nil } ; (code) => [:y, :s]
a = { :i => nil } ; (code) => [:s, :y] i = { :a => nil } ; (code) => [:s, :y]
a = { :j => nil } ; (code) => [:y, :s] j = { :a => nil } ; (code) => [:y, :s]
a = { :k => nil } ; (code) => [:s, :y] k = { :a => nil } ; (code) => [:s, :y]
a = { :l => nil } ; (code) => [:s, :y] l = { :a => nil } ; (code) => [:s, :y]
a = { :m => nil } ; (code) => [:s, :y] m = { :a => nil } ; (code) => [:s, :y]
a = { :n => nil } ; (code) => [:s, :y] n = { :a => nil } ; (code) => [:s, :y]
a = { :o => nil } ; (code) => [:y, :s] o = { :a => nil } ; (code) => [:y, :s]
a = { :p => nil } ; (code) => [:s, :y] p = { :a => nil } ; (code) => [:s, :y]
a = { :q => nil } ; (code) => [:y, :s] q = { :a => nil } ; (code) => [:y, :s]
a = { :r => nil } ; (code) => [:y, :s] r = { :a => nil } ; (code) => [:y, :s]
a = { :s => nil } ; (code) => [:s, :y] s = { :a => nil } ; (code) => [:s, :y]
a = { :t => nil } ; (code) => [:s, :y] t = { :a => nil } ; (code) => [:s, :y]
a = { :u => nil } ; (code) => [:y, :s] u = { :a => nil } ; (code) => [:y, :s]
a = { :v => nil } ; (code) => [:s, :y] v = { :a => nil } ; (code) => [:s, :y]
a = { :w => nil } ; (code) => [:y, :s] w = { :a => nil } ; (code) => [:y, :s]
a = { :x => nil } ; (code) => [:y, :s] x = { :a => nil } ; (code) => [:y, :s]
a = { :y => nil } ; (code) => [:s, :y] y = { :a => nil } ; (code) => [:s, :y]
a = { :z => nil } ; (code) => [:y, :s] z = { :a => nil } ; (code) => [:y, :s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment