Skip to content

Instantly share code, notes, and snippets.

@u2
Last active December 31, 2015 14:19
Show Gist options
  • Save u2/7999546 to your computer and use it in GitHub Desktop.
Save u2/7999546 to your computer and use it in GitHub Desktop.
Once
# 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