Skip to content

Instantly share code, notes, and snippets.

@gabetax
Last active December 19, 2015 00:29
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 gabetax/5869080 to your computer and use it in GitHub Desktop.
Save gabetax/5869080 to your computer and use it in GitHub Desktop.
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,...>
@cbandy
Copy link

cbandy commented Jun 26, 2013

&:foo evaluates to:

{ |x| x.foo }

@gabetax
Copy link
Author

gabetax commented Jun 26, 2013

@cbandy you're totally right. not sure if i'm going to attribute that as a typo or brain fart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment