1

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?

karkum
  • 427
  • 4
  • 16
  • 1
    I ran your code using the exact same build (1.8.7 patchlevel 358), albeit on a newer OS X (Mavericks=13.0.0) and got 9183. Also tried a one-liner to see if it made a difference, but it did not ( `set = (2..100).inject([]) { |a,i| (2..100).inject(a) { |a,j| a << i**j } }.uniq` ). – Mark Reed Dec 15 '13 at 05:22

1 Answers1

1

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.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Hmmmm this is really weird. I just tried it again, running the same commands as you, and I still get 8243 for the old version and 9183 for the newer version... – karkum Dec 15 '13 at 05:00
  • 1
    @karkum, What do you get with `puts set.size` just before call `uniq!` ? It should be `9801`. – falsetru Dec 15 '13 at 05:03
  • Yep, it is 9801. You think it has to do something with the implementation of `uniq`? – karkum Dec 15 '13 at 05:05
  • 1
    @karkum, It's interesting. I ran it with the exactly same version (1.8.7 (2012-02-08 patchlevel 358)) as you. (except OS). – falsetru Dec 15 '13 at 05:19
  • Yes it is. I'm not too worried about it, 1.8 will be obsolete soon. [Here is a similar question that might explain it](http://stackoverflow.com/questions/8653508/arrayuniq-with-block-equivalent-in-ruby-1-8-7) – karkum Dec 15 '13 at 05:22