{
  "$type": "site.standard.document",
  "canonicalUrl": "https://deterministic.space/ruby-ufcs.html",
  "path": "/ruby-ufcs.html",
  "publishedAt": "2017-12-17T00:00:00.000Z",
  "site": "at://did:plc:x67qh7v3fd7znbdhauc45ng3/site.standard.publication/3mjcd2t6afe25",
  "textContent": "As someone who likes writing code in a functional style,\nI really enjoy passing functions as arguments.\nWhether in Rust or in JavaScript,\nfirst-class functions are very useful.\nSadly, as naming a function/method in Ruby is the same\nas calling it with zero parameters,\nthis at first seems impossible to do.\n\nWhat you can do is pass ~~closures~~ blocks methods.\nE.g., Array#map really likes being called with a block:\n\nIt's not the same as passing an actual function…\nbut there is one built-in shortcut:\nYou can call the methods defined for the item's class\n(this calls String#size):\n\nThis works quite well for a lot of cases and is very useful in practice.\nThis is also probably 95% of what a regular Ruby code will ever need.\nBut, you know, it wasn't enough.\nI had to dig deeper.\n\nThe thing is:\nI sometimes just want to write simple modules that contain functions.\n\nI'm not saying this is idiomatic Ruby,\nbut it often is exactly what I want.\nNot a singleton class whose name ends with Service, just a module.\nNot adding a method to Integer, just a function in a ~~namespace~~ module.\nNot adding this module as a mixin to a class defined in my application\n(and not writing a wrapper class if the item's class is not defined in my application).\nAnd not a { |x| Foo::bar(x) }, I just want Foo::bar.\n\nSo, is this possible?\nYes, kinda:\n\nThe method method gives you, well, a [Method].\nIt's meta programming at its finest, and,\naside from obvious things like arity and name,\nhas some interesting methods, like curry, owner, and source.\nAnd the official documentation also abbreviates it with \"meth\".\nJust saying.\n\n[Method]: https://ruby-doc.org/core-2.3.1/Method.html\n\nBut, back to the problem at hand:\nIs this ugly as hell and should you never ever use it?\nMost likely, yes.\n\nSo, how to make it better?\nWith more weird meta programming, of course!\nLet's add a method $m_fn\nthat automatically gives the Method of $m.\nIf you liked my rambling on\n[doing weird things with Rust traits][trait-shenanigans]\nmore than my [Rust flavored Ruby][rust-and-ruby] post,\nyou will love what follows.\nIf you want to keep your ~~sanity~~ innocence, close this tab right now.\n\n[rust-and-ruby]: https://deterministic.space/rust-and-ruby.html\n[trait-shenanigans]: https://deterministic.space/impl-a-trait-for-str-slices-and-slices-of-strs.html\n\nYeah, that  chain of end lines\nthat make even the most experienced LISP programmers blush\nis a good indicator of how many tries it took me to get this to work.\nBut it does work:\n\n[See it in action here.][repl]\n\nAnd finally: Should you ever use this? Let's just say I hope you don't need to…\n\nUpdate: It just occurred to me that\npeople might want to read even more on this\n~~weird~~ interesting aspect of Ruby.\nHere you go:\n\n- What does the & in &Foo::bar_fn even do?\n- Why does &(:fn) work?\n- Dynamically adding methods (What could possibly go wrong?)\n\n[repl]: https://repl.it/repls/MonumentalStylishOlingo \"repl.it chose the random name Monumental Stylish Olingo for this snippet of code. Very fitting\"",
  "title": "Passing Functions as Parameters in Ruby",
  "updatedAt": "2017-12-17T00:00:00.000Z"
}