Re: Counting Toothpicks (#111)


#1

I have a fairly verbose submission for this quiz. It’s probably quite
slow compared to others, but at least I can follow my own code :slight_smile: One
question for the more advanced rubyists - I use sets in one place, and
tried to unit test the results, but Set.== didn’t perform as I would
have expected… for instance:

require ‘set’
s1 = Set.new
s1 << {1=>2}
s1 << {2=>3}
s2 = Set.new
s2 << {1=>2}
s2 << {2=>3}
s1 == s2

is false. Presumably this is due to reference v.s. values equality, but
I thought that since Hash defines values-based ==, Set.== would use it,
but it appears not. This is somewhat surprising, I’ll warrant.

Oh, and what’s the preferred submission method - attachment, inclusion,
or pastie?

  • donald

#2

Ball, Donald A Jr (Library):

One question for the more advanced rubyists

Let a newbie answer. :slight_smile: For a similar question, see my ‘Sane
#hash implementation?’ mail sent just a couple minutes ago.

I use sets in one place, and tried to unit test the results, but
Set.== didn’t perform as I would have expected… for instance:

Set#== performs as advertised.

Set.new == Set.new
=> true

Set.new([1,2]) == Set.new([1,2])
=> true

require ‘set’
s1 = Set.new
s1 << {1=>2}
s1 << {2=>3}
s2 = Set.new
s2 << {1=>2}
s2 << {2=>3}
s1 == s2

is false.

I thought that since Hash defines values-based ==,
Set.== would use it, but it appears not.

Set#== docs say, ‘the equality of each couple of elements
is defined according to Object#eql?,’ and seem to be right:

Hash.new({1=>2}).eql? Hash.new({1=>2})
=> false

– Shot


#3

On Jan 29, 2007, at 4:16 PM, Ball, Donald A Jr (Library) wrote:

Oh, and what’s the preferred submission method - attachment,
inclusion,
or pastie?

I personally think inline code is the best for the purposes of the
archives.

I’ve enjoyed the habit some are getting into by providing the pastie
link as an option though. With that, syntax highlighting is only a
click away and I can download your source without email reformatting
concerns.

I’m pretty easy going though and try to accept everything.

James Edward G. II