From: "trans (Thomas Sawyer)" Date: 2013-08-20T19:26:24+09:00 Subject: [ruby-core:56756] [ruby-trunk - Feature #8804] ONCE syntax Issue #8804 has been updated by trans (Thomas Sawyer). A use case is memoization. Instead of def q @q ||= (...some long calc...) end You could do def q ONCE { ...some long calc... } end ---------------------------------------- Feature #8804: ONCE syntax https://bugs.ruby-lang.org/issues/8804#change-41299 Author: ko1 (Koichi Sasada) Status: Open Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: syntax Target version: next minor How about to introduce ONCE{...} syntax which do block only once and return the first value? - It is similar of BEGIN{} and END{}. - General syntax of /reg/o. ## simulation code ONCE_CACHE = {} ONCE_MUTEX = Mutex.new def ONCE(&b) raise unless block_given? ONCE_MUTEX.synchronize{ key = caller(3, 1)[0] if value = ONCE_CACHE[key] value else ONCE_CACHE[key] = yield end } end 3.times{|i| p ONCE{ i #=> every time 0 } } ## ## Note that this code doesn't work if two or more ONCE{} are available in one line. -- http://bugs.ruby-lang.org/