Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Rails group_by deprecated?

I have an array that I want to group and it seems like the 'group_by' function is OK for my situation.

http://apidock.com/rails/Enumerable/group_by

I used it in Rails 3.2.13.

grouped_array = my_array.group_by(&:my_function)

# Assume run 'my_function' have result1 on element1, element3 and result2 on element2, element4, then: 
    # grouped_array = {
    #    result1 => [element1, element3],
    #    result2 => [element2, element4],
    #    ...
    # }

But I see it's deprecated in 4.0.2.

Which function should I use?

like image 910
vietstone Avatar asked Aug 15 '14 10:08

vietstone


1 Answers

It's not deprecated.

Ruby prior to 1.8.7 didn't have group_by builtin, so rails added it. Ruby 1.8.7 added group_by, but whereas the rails group_by returned ordered hashes, the ruby 1.8.7 returned plain hashes (since ordered hashes weren't yet in ruby 1.8.7), so rails continued to overwrite the method.

Ruby 1.9 did have group_by, so rails no longer needed to overwrite it, but this code was left there for people still running 1.8.7.

Rails 4 dropped ruby 1.8.7 compatibility, so code like this was removed from rails. It's not deprecated, but it's no longer in rails because it has become part of the ruby standard library.

like image 119
Frederick Cheung Avatar answered Sep 19 '22 15:09

Frederick Cheung