"once" methods

Discussion in 'Ruby' started by Steven Arnold, Sep 24, 2005.

  1. Hi,

    I was reading PickAxe and came across the very cool concept of "once"
    methods, which are methods that only evaluate when first called,
    returning a cached value for subsequent calls.

    Looking around a little on the Internet, I see a note that rolling
    this into Ruby's standard library was approved at some point,
    something that seems like a good idea to me. However, I can't
    immediately find where this functionality would be located. Google
    searches for "once" and "method" and "Ruby" understandably turn up a
    great many results that have nothing to do with what I'm looking for.

    Has this been rolled into the standard library? If so, where?

    Thanks in advance,
    steve
     
    Steven Arnold, Sep 24, 2005
    #1
    1. Advertising

  2. Steven Arnold

    Trans Guest

    Maybe #memoize.
     
    Trans, Sep 24, 2005
    #2
    1. Advertising

  3. On Sep 24, 2005, at 12:57 AM, Steven Arnold wrote:

    > Hi,
    >
    > I was reading PickAxe and came across the very cool concept of
    > "once" methods, which are methods that only evaluate when first
    > called, returning a cached value for subsequent calls.


    This is usually called memoization, but I'm not aware of a standard
    library that handles it.

    James Edward Gray II
     
    James Edward Gray II, Sep 24, 2005
    #3
  4. Steven Arnold

    Trans Guest

    I looked at the RCR, I think once is something a little more low level
    than memoize:

    a = nil
    x = "FOO"
    10.times do
    a = once { "#{x}" }
    x = "BAR"
    end
    p a #=> "FOO"

    Akin to persistant locals:

    def x
    arr = []
    %a ||= 0
    2.times do
    arr << %a += 1
    end
    arr
    end

    x #=> [1, 2]
    x #=> [3, 4]

    Hmm.. actaually this might help solve the whole ||= thing.

    module
    def module_info
    @module_info = once{ {} }
    end
    end

    T.
     
    Trans, Sep 24, 2005
    #4
  5. Steven Arnold

    Rob Rypka Guest

    ------=_Part_19214_31744150.1127575604797
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 9/24/05, James Edward Gray II <> wrote:
    >
    > On Sep 24, 2005, at 12:57 AM, Steven Arnold wrote:
    >
    > > Hi,
    > >
    > > I was reading PickAxe and came across the very cool concept of
    > > "once" methods, which are methods that only evaluate when first
    > > called, returning a cached value for subsequent calls.

    >
    > This is usually called memoization, but I'm not aware of a standard
    > library that handles it.
    >


    http://raa.ruby-lang.org/project/memoize/

    I havent yet tried it out, but if this doesn't work, it's not too hard to
    implement.

    --
    Rob

    ------=_Part_19214_31744150.1127575604797--
     
    Rob Rypka, Sep 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    270
    limodou
    Dec 4, 2006
  2. Iván Vega Rivera
    Replies:
    6
    Views:
    138
    Iván Vega Rivera
    Mar 8, 2006
  3. Trans
    Replies:
    25
    Views:
    300
    Wilson Bilkovich
    Mar 22, 2006
  4. Kenneth McDonald
    Replies:
    5
    Views:
    377
    Kenneth McDonald
    Sep 26, 2008
  5. Gancy
    Replies:
    4
    Views:
    241
    Rasto Levrinc
    Feb 3, 2005
Loading...

Share This Page