Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 2, 2019 09:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/37f833c0a02d14d7ee7e97159c59afbf to your computer and use it in GitHub Desktop.
Save lbvf50mobile/37f833c0a02d14d7ee7e97159c59afbf to your computer and use it in GitHub Desktop.
Greetings to @Morozzzko, the Ruby is neat.

Hi folks here I show some Ruby magic.

  • a.group_by{|x| x} is similar to a.group_by(&:itself)
  • a.group_by(&:itself) is a short cut for a.group_by(&:itself.to_proc)
  • and a.group_by(&:itself.to_proc) just use new Proc a.group_by(&Proc.new{|o| o.send :itself})
  • a.group_by(&Proc.new{|o| o.send :itself}) use & to treat a Proc as block like this a.group_by{|o| o.send :itself}
  • a.group_by{|o| o.send :itself} here it's possible to substitute o.send :itself to o.itself: a.gruop_by{|o| o.itself}
  • a.group_by{|o| o.itself} here o.itself it's just an o so it's equal to a.group_by{|o| o}
  • Hey and the magic is a.group_by{|o| o} is just a a.group_by{|x| x}
# 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