Difference in output between Ruby versions when running script

61 views Asked by At

I have the following script in ruby:

set = []
for i in 2..100 
  for j in 2..100
    set << i**j
  end 
end
puts set.uniq!.count

When running this script with Ruby version ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0], the output is 8243.

When I run this script with Ruby version ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.4.0], the output is 9183 (which is what I expect the result to be).

Any ideas why there is a discrepancy between the two versions?

1

There are 1 answers

5
falsetru On BEST ANSWER

I get same results for both version (1.8, 1.9.3)

$ ruby1.8 --version
ruby 1.8.7 (2012-02-08 patchlevel 358) [i686-linux]
$ ruby1.9.3 --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]
$ ruby1.8 t.rb
9183
$ ruby1.9.3 t.rb
9183

BTW, chaining uniq! with count is not a good idea, because uniq! returns nil if there's no duplicate.