Last active
October 14, 2019 14:21
-
-
Save daramcq/f87a0cd96e7e141c28b36ce302b7a09e to your computer and use it in GitHub Desktop.
This file contains 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
# Decorating an anonymous function using a block is fairly easy | |
log_function = lambda do |fn, *args| | |
puts "About to call" | |
fn.call(*args) | |
puts "Did the call" | |
end | |
arrow_function = -> (*args) { puts "Being called now with #{args}" } | |
arrow_function = log_function.curry.(arrow_function) | |
arrow_function | |
# To make it work with a normal function, we need to do... | |
def normal_function(*args) | |
puts "Being called now with #{args}" | |
end | |
modified_log_function = lambda do |fn_symbol, *args| | |
puts "About to call" | |
method(fn_symbol).call(*args) | |
puts "Did the call" | |
end | |
normal_function = modified_log_function.curry.(:normal_function) | |
normal_function | |
# This seems like the best way to do decoration. | |
module Decorator | |
def hello(*) | |
puts "Doing a thing" | |
super | |
puts "Done the thing" | |
end | |
end | |
MyObj = Struct.new(:name) do | |
def hello | |
puts "My name is #{name}" | |
end | |
end | |
MyObj.prepend Decorator | |
MyObj.new("Dara").hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment