# Date class method \`once\` wraps original call in Array. Why?

**URL:** <https://www.ruby-forum.com/t/date-class-method-once-wraps-original-call-in-array-why/179590>\
**Category:** Ruby\
**Created:** [November 29, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/date-class-method-once-wraps-original-call-in-array-why/179590 "2009-11-29T20:51:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [November 29, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/date-class-method-once-wraps-original-call-in-array-why/179590/1 "2009-11-29T20:51:10Z")

</div>

I’m reading the Pickaxe and on page 391, there is a code listing for  
the class method `once` from the Date module in Ruby:

def once(\*ids) #:nodoc:  
for id in ids  
module\_eval \<\<-“end:”  
alias\_method : **#{id.to\_i}** , :#{id.to\_s}  
private : **#{id.to\_i}**  
def #{id.to\_s}(\*args, &block)  
puts “Inside once: #{args.inspect}”  
(@ **#{id.to\_i}** ||= [**#{id.to\_i}** (\*args, &block)])[0]  
end  
end:  
end  
end

I was wondering why the original method called is wrapped in an Array.  
So I started digging the the source history hoping to find an  
explanation. I found the commit that changed it to use the Array but  
no explanation for the change. The commit where it was modified is  
here:

[http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/date2.rb?r1=710&r2=709&pathrev=710](http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/date2.rb?r1=710&r2=709&pathrev=710)

I created a simple class and use the code with and without the Array  
and it seems to work the same for my simple test. Just curious if I’m  
not seeing something obvious.

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 1, 2009, 5:07am UTC](https://www.ruby-forum.com/t/date-class-method-once-wraps-original-call-in-array-why/179590/2 "2009-12-01T05:07:51Z")

</div>

They wrap it in an array so you that they can cache “nil” and “false”  
return values:

result ||= expensive\_computation

will still run the expensive computation if it returned nil or false,  
however, if you did:

(result ||= [expensive\_computation])[0],

you could cache nil as a return value and it wouldn’t actually run the  
expensive computation.

Matt Haley wrote:

> I’m reading the Pickaxe and on page 391, there is a code listing for  
> the class method `once` from the Date module in Ruby:
> 
> def once(\*ids) #:nodoc:  
> for id in ids  
> module\_eval \<\<-“end:”  
> alias\_method : **#{id.to\_i}** , :#{id.to\_s}  
> private : **#{id.to\_i}**  
> def #{id.to\_s}(\*args, &block)  
> puts “Inside once: #{args.inspect}”  
> (@ **#{id.to\_i}** ||= [**#{id.to\_i}** (\*args, &block)])[0]  
> end  
> end:  
> end  
> end
> 
> I was wondering why the original method called is wrapped in an Array.  
> So I started digging the the source history hoping to find an  
> explanation. I found the commit that changed it to use the Array but  
> no explanation for the change. The commit where it was modified is  
> here:
> 
> [http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/date2.rb?r1=710&r2=709&pathrev=710](http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/date2.rb?r1=710&r2=709&pathrev=710)
> 
> I created a simple class and use the code with and without the Array  
> and it seems to work the same for my simple test. Just curious if I’m  
> not seeing something obvious.
