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
# Если ты создаешь новую функцию, а не меняешь старую, то всё проще, конечно. | |
def nillified(&block) | |
->(first_arg, *args) { | |
first_arg ? block.call(first_arg, *args) : nil | |
} | |
end | |
f = nillified{|x,y| x + y } |
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
module DefNil | |
def defnil(meth) | |
self.class.class_eval do | |
alias_method "#{meth}_before_defnil", meth | |
define_method meth do |first_arg, *args, &block| | |
if first_arg | |
send("#{meth}_before_defnil", first_arg, *args, &block) | |
else | |
nil | |
end |