|
# 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 π₯" |