Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active May 30, 2019 06:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoshCheek/ea50a9691ea11ad720f9b4d270e8a656 to your computer and use it in GitHub Desktop.
Save JoshCheek/ea50a9691ea11ad720f9b4d270e8a656 to your computer and use it in GitHub Desktop.
Evil Ruby: Turning procs into first class functions
  • To read the original tweet and comments, click here.
  • For my followup abomination, click here
  • If you're thinking about treating this as anything other than art, read there πŸ‘‡

DISCLAIMER:

THIS IS ART, DON'T ACTUALLY DO THIS IN ANY CODE THAT YOU CARE ABOUT!

☠️ This is not a place of honor ☠️

☠️ Nothing of esteem is commemorated here ☠️

☠️ Nothing valued is here ☠️

~~ Expert Judgment on Markers To Deter Inadvertant Human Intrusion Into the Waste Isolation Pilot Plant

# Okay, so, we've got us a lambda, check it out:
fancify = -> x { "✨ #{x} ✨" }
# But how to call it? like this? Psh...
fancify.("2nd class") # => "✨ 2nd class ✨"
# What do we want?πŸ’₯ 1st class functions!πŸ’₯ When do we want them? Now!
fancify("1st class") rescue $! # => #<NoMethodError: undefined method `fancify' for main:Object>
# 😞
# πŸ˜‘
# 😐
# 😠
# 😑
# πŸ‘Ώ
# 😈 ...fire flew from his fingertips as he rosined up his bow
require "binding_of_caller"
def method_missing(name, *args, &block)
(bnd = binding.of_caller 1) &&
(bnd.local_variable_defined? name) &&
(local = bnd.local_variable_get name) &&
(local.respond_to? :call) ?
(local.call *args, &block) :
super
end
# Hell's broke loose in Ruby, and the devil deals the cards!
fancify = -> x { "✨ #{x} ✨" }
fancify.("2nd class") # => "✨ 2nd class ✨"
fancify("1st class!") # => "✨ 1st class! ✨"
fancify "🀘😈🀘" # => "✨ 🀘😈🀘 ✨"
sing = -> lyric { "πŸ”₯#{lyric.center 30}πŸ”₯" }
sing("Fire on the Mountain") # => "πŸ”₯ Fire on the Mountain πŸ”₯"
sing.("run boys, run") # => "πŸ”₯ run boys, run πŸ”₯"
sing["The devil's in the House"] # => "πŸ”₯ The devil's in the House πŸ”₯"
sing.[] "of the Rising Sun" # => "πŸ”₯ of the Rising Sun πŸ”₯"
sing.call "Chicken in a bread pan" # => "πŸ”₯ Chicken in a bread pan πŸ”₯"
sing === "making a nest" # => "πŸ”₯ making a nest πŸ”₯"
sing.yield "Granny, does your dog bite?" # => "πŸ”₯ Granny, does your dog bite? πŸ”₯"
sing::("Yes, child, yes") # => "πŸ”₯ Yes, child, yes πŸ”₯"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment