Forum: Ruby Re: Counting Toothpicks (#111)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E1f43bafda26307a050d11902752b2a6?d=identicon&s=25 Ball, Donald A Jr (Library) (Guest)
on 2007-01-29 23:17
(Received via mailing list)
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 :) 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
Cec345a59245af9d06e4438a413f4eb5?d=identicon&s=25 Shot (Piotr Szotkowski) (Guest)
on 2007-01-29 23:57
(Received via mailing list)
Ball, Donald A Jr (Library):

> One question for the more advanced rubyists

Let a newbie answer. :) 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-01-30 00:07
(Received via mailing list)
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 Gray II
This topic is locked and can not be replied to.