uniqueness of keys in group_by

Discussion in 'Ruby' started by cribe, Aug 4, 2011.

  1. cribe

    cribe Guest

    Ok, I have a class that extends array but I'm seeing bizarre behavior when I try to do group_by on it.

    # first I thought, maybe it uses object_id
    "a".object_id == "a".object_id #=> false
    ["a", "b", "a"].group_by {|i| i} #=> #<OrderedHash {"a"=>["a", "a"], "b"=>["b"]}>

    #ok, that's not it. maybe inspect()?
    class Klass
    def initialize(v)
    @value = v
    end
    def inspect
    @value
    end
    end
    arr = []
    (0..10).each do |x|
    arr << Klass.new(rand(3))
    end
    arr.group_by {|i| i} #=> #<OrderedHash {2=>[2], 1=>[1], 0=>[0], 2=>[2], 1=>[1], 1=>[1], 2=>[2], 0=>[0], 2=>[2], 2=>[2], 0=>[0]}>

    #nope. and yet:
    [2, 1, 0, 2, 1, 1, 2, 0, 2, 2, 0].group_by {|i| i} #=>#<OrderedHash {0=>[0, 0, 0], 1=>[1, 1, 1], 2=>[2, 2, 2, 2, 2]}>

    # however, if I explicitly call inspect then it works
    arr.group_by {|i| i.inspect} #=> #<OrderedHash {0=>[0, 0, 0], 1=>[1, 1, 1], 2=>[2, 2, 2, 2, 2]}>

    Any ideas?

    (BTW this was all being done in a rails console which is why the results were returning orderedhashes instead of regular hashes but the results are the same in irb anyway)
    cribe, Aug 4, 2011
    #1
    1. Advertising

  2. On 04.08.2011 22:03, cribe wrote:
    > Ok, I have a class that extends array but I'm seeing bizarre behavior
    > when I try to do group_by on it.
    >
    > # first I thought, maybe it uses object_id "a".object_id ==
    > "a".object_id #=> false ["a", "b", "a"].group_by {|i| i} #=>
    > #<OrderedHash {"a"=>["a", "a"], "b"=>["b"]}>
    >
    > #ok, that's not it. maybe inspect()? class Klass def initialize(v)
    > @value = v end def inspect @value end end arr = [] (0..10).each do
    > |x| arr<< Klass.new(rand(3)) end arr.group_by {|i| i} #=>
    > #<OrderedHash {2=>[2], 1=>[1], 0=>[0], 2=>[2], 1=>[1], 1=>[1],
    > 2=>[2], 0=>[0], 2=>[2], 2=>[2], 0=>[0]}>
    >
    > #nope. and yet: [2, 1, 0, 2, 1, 1, 2, 0, 2, 2, 0].group_by {|i| i}
    > #=>#<OrderedHash {0=>[0, 0, 0], 1=>[1, 1, 1], 2=>[2, 2, 2, 2, 2]}>
    >
    > # however, if I explicitly call inspect then it works arr.group_by
    > {|i| i.inspect} #=> #<OrderedHash {0=>[0, 0, 0], 1=>[1, 1, 1],
    > 2=>[2, 2, 2, 2, 2]}>
    >
    > Any ideas?


    $ ri19 Enumerable#group_by
    Enumerable#group_by

    (from ruby core)
    ------------------------------------------------------------------------------
    enum.group_by {| obj | block } -> a_hash
    enum.group_by -> an_enumerator

    ------------------------------------------------------------------------------

    Returns a hash, which keys are evaluated result from the block, and
    values are
    arrays of elements in enum corresponding to the key.

    If no block is given, an enumerator is returned instead.

    (1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}


    Hash keys are evaluated based on #hash and #eql? methods.

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Aug 6, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ognen Ivanovski
    Replies:
    0
    Views:
    939
    Ognen Ivanovski
    Jul 15, 2003
  2. Michael Neumann

    [RCR] Enumerable#group_by

    Michael Neumann, May 25, 2004, in forum: Ruby
    Replies:
    2
    Views:
    116
  3. Max Williams
    Replies:
    5
    Views:
    174
    Robert Klemme
    Sep 9, 2008
  4. Adam Gardner
    Replies:
    5
    Views:
    141
    Sebastian Hungerecker
    Nov 19, 2008
  5. Dave Smith

    hash of hashes group_by

    Dave Smith, Jun 5, 2009, in forum: Ruby
    Replies:
    1
    Views:
    130
    Brian Candler
    Jun 5, 2009
Loading...

Share This Page