Last active
December 31, 2015 14:19
-
-
Save u2/7999546 to your computer and use it in GitHub Desktop.
Once
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
# once/once.rb: Written by Tadayoshi Funaba 1998,2000,2002,2006,2008 | |
# $Id: once.rb,v 1.2 2008-11-12 19:58:01+09 tadf Exp $ | |
module Once | |
def once(*ids) | |
for id in ids | |
module_eval <<-"end;" | |
alias_method :__#{id.object_id}__, :#{id.to_s} | |
def #{id.to_s}(*args, &block) | |
(@__#{id.object_id}__ ||= [__#{id.object_id}__(*args, &block)])[0] | |
end | |
end; | |
end | |
end | |
private :once | |
def once_class_method(*ids) | |
for id in ids | |
module_eval <<-"end;" | |
class << self | |
alias_method :__#{id.object_id}__, :#{id.to_s} | |
def #{id.to_s}(*args, &block) | |
(@__#{id.object_id}__ ||= [__#{id.object_id}__(*args, &block)])[0] | |
end | |
end | |
end; | |
end | |
end | |
private :once_class_method | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment