Last active
December 19, 2015 00:29
-
-
Save gabetax/5869080 to your computer and use it in GitHub Desktop.
This file contains hidden or 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://twitter.com/garybernhardt/status/349920138889404416 | |
[1] pry(main)> class Object; alias_method :&, :method; end | |
=> Object | |
With standard symbol to proc, &:foo evaluates to: | |
{ |x| x.foo } | |
With the "pretzel bun", &foo&:bar evaluates to: | |
{ |x| foo.bar x } | |
Impractical, but more relatable example: | |
[2] pry(main)> (1..3).map &User&:find | |
User Load (36.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] | |
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]] | |
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 3]] | |
=> [#<User id: 1,...>, | |
#<User id: 2,...>, | |
#<User id: 3,...] | |
[3] pry(main)> User.method :find | |
=> #<Method: Class(ActiveRecord::Querying)#find> | |
[4] pry(main)> User&:find | |
=> #<Method: Class(ActiveRecord::Querying)#find> | |
[5] pry(main)> (User&:find).to_proc | |
=> #<Proc:0x00000006c0b628 (lambda)> | |
[6] pry(main)> (User&:find).to_proc.call 1 | |
User Load (2.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] | |
=> #<User id: 1,...> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
&:foo
evaluates to: