a.group_by{|x| x}
is similar toa.group_by(&:itself)
a.group_by(&:itself)
is a short cut fora.group_by(&:itself.to_proc)
- and
a.group_by(&:itself.to_proc)
just use newProc
a.group_by(&Proc.new{|o| o.send :itself})
a.group_by(&Proc.new{|o| o.send :itself})
use&
to treat aProc
asblock
like thisa.group_by{|o| o.send :itself}
a.group_by{|o| o.send :itself}
here it's possible to substituteo.send :itself
too.itself
:a.gruop_by{|o| o.itself}
a.group_by{|o| o.itself}
hereo.itself
it's just ano
so it's equal toa.group_by{|o| o}
- Hey and the magic is
a.group_by{|o| o}
is just aa.group_by{|x| x}
Last active
July 2, 2019 09:55
-
-
Save lbvf50mobile/37f833c0a02d14d7ee7e97159c59afbf to your computer and use it in GitHub Desktop.
Greetings to @Morozzzko, the Ruby is neat.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/a/12725405/8574922 | |
# https://apidock.com/ruby/Enumerable/group_by | |
p [1,1,1,1,1,2,2,2,15,15,20,25,25,40,4,4].group_by{|x| x}.values | |
p [1,1,1,1,1,2,2,2,15,15,20,25,25,40,4,4].group_by(&:itself).values # @morozzzko | |
p [1,1,1,1,1,2,2,2,15,15,20,25,25,40,4,4].group_by(&:itself.to_proc).values | |
p [1,1,1,1,1,2,2,2,15,15,20,25,25,40,4,4].group_by(&Proc.new{|o| o.send :itself}).values | |
p [1,1,1,1,1,2,2,2,15,15,20,25,25,40,4,4].group_by{|o| o.send :itself}.values | |
p [1,1,1,1,1,2,2,2,15,15,20,25,25,40,4,4].group_by{|o| o.itself}.values | |
p [1,1,1,1,1,2,2,2,15,15,20,25,25,40,4,4].group_by{|o| o }.values | |
# https://goo.gl/yyYoHD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment