Skip to content

Instantly share code, notes, and snippets.

View VorontsovIE's full-sized avatar

Ilya Vorontsov VorontsovIE

View GitHub Profile
@VorontsovIE
VorontsovIE / defnil.rb
Last active December 16, 2015 11:29 — forked from sherpc/defnil.clj
# Если ты создаешь новую функцию, а не меняешь старую, то всё проще, конечно.
def nillified(&block)
->(first_arg, *args) {
first_arg ? block.call(first_arg, *args) : nil
}
end
f = nillified{|x,y| x + y }
@VorontsovIE
VorontsovIE / defnil.rb
Last active December 16, 2015 11:29 — forked from sherpc/defnil.rb
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