Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > uniqueness of keys in group_by

Reply
Thread Tools

uniqueness of keys in group_by

 
 
cribe
Guest
Posts: n/a
 
      08-04-2011
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)
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-06-2011
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hashes group_by Dave Smith Ruby 1 06-05-2009 03:06 PM
Subclassing Hash to enforce value uniqueness ala key uniqueness. Adam Gardner Ruby 5 11-19-2008 07:36 AM
Is this a sensible implementation for Array#group_by ? Max Williams Ruby 5 09-09-2008 09:36 AM
[RCR] Enumerable#group_by Michael Neumann Ruby 2 05-25-2004 08:24 PM
XML Schema keys, uniqueness based on ancestor's attribute Ognen Ivanovski XML 0 07-15-2003 02:36 PM



Advertisments