From: Sam Kong Date: 2007-08-17T05:59:58+09:00 Subject: Re: Lazy function definition pattern in Ruby? On Aug 16, 1:20 pm, "Chris Carter" wrote: > On 8/16/07, dbl...@rubypal.com wrote: > > > You could do this (and I think it's similar to the "once" technique > > [pattern?] that's used in the Date library and talked about in the > > Pickaxe): > > > def my_time > > t = Time.now > > (class << self; self; end).class_eval do > > define_method(:my_time) { t } > > end > > t > > end > > Why not just use a closure, and do it the way you would in JS? > > foo = proc { x=Time.now; foo = proc { x } }; foo.call > > Then you just access it with foo[]/foo.call > > -- > Chris Carter > concentrationstudios.com > brynmawrcs.com I also thought of it. But foo.call isn't so good. I think JavaScript's syntax is better than ruby's about returning a function. Sam