SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is ruby pulling my leg?

    The code:
    Code:
              $stderr.print("e = #{e.inspect}\ndidnot add new DFA state #{state_map.keys.include?(e)}\n#{state_map.inspect}\n")
              e_new_state = state_map[e];
              $stderr.print("===#{e_new_state}===\n")
    The output
    Code:
    e = #<Set: {0, 1, 2, 3}>
    didnot add new DFA state true
    {#<Set: {0, 1, 2, 3}>=>5, #<Set: {0, 2, 3}>=>4}
    ======
    I'm at a loss to imagine why e_new_state IS NOT 5.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code ruby:
    >> require 'set'
    => true
    >> a = Set.new
    => #<Set: {}>
    >> b = Set.new
    => #<Set: {}>
    >> a.hash
    => 24386660
    >> b.hash
    => 24383900

    Conclusion: don't put sets in hash tables

  3. #3
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any alternatives you'd suggest?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Implement a real .hash for sets:

    Code ruby:
    >> require 'set'
    => true
    >> class Set
    >> def hash
    >> to_a.hash
    >> end
    >> end
    => nil
    >> a = Set.new
    => #<Set: {}>
    >> b = Set.new
    => #<Set: {}>
    >> a.hash
    => 0
    >> b.hash
    => 0
    >> a << 2
    => #<Set: {2}>
    >> a << 3
    => #<Set: {2, 3}>
    >> b << 3
    => #<Set: {3}>
    >> b << 2
    => #<Set: {2, 3}>
    >> a.hash
    => 5
    >> b.hash
    => 5

    So just put the .hash implementation in your file:

    Code ruby:
    class Set
      def hash
        to_a.hash
      end
    end
    Last edited by Fenrir2; Mar 16, 2008 at 07:40.

  5. #5
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    meh I ended up creating a new class that inherited from Set. I over-rid the hash and eql?() functions and it worked like a charm.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •