Constant domain and procs

Discussion in 'Ruby' started by David Espada, Sep 21, 2011.

  1. David Espada

    David Espada Guest

    Hi.

    I have a question about constants in Ruby. Look at this code:

    -------------------------------------
    class Pepo
    POPORRES = 4

    def self.pepo(&block)
    Pepo.new(&block)
    end

    def initialize(&block)
    instance_eval(&block)
    end
    end

    Pepo.pepo do
    puts POPORRES
    end

    -------------------------------------

    When executing, it fails with erro 'uninitialized constant POPORRES'. If proc
    executes in Pepo class context, why it fails so?

    Thank you very much for your aclarations.

    Greets.
     
    David Espada, Sep 21, 2011
    #1
    1. Advertisements

  2. Constant lookup is done statically. However things have changed with
    1.9 and there your test works. I don't have a reference handy but if
    you search for changes in 1.9 you'll likely find material.

    Kind regards

    robert
     
    Robert Klemme, Sep 21, 2011
    #2
    1. Advertisements

  3. David Espada

    David Espada Guest

    El miércoles 21 de septiembre, Robert Klemme escribió:
    I have tested it and works perfectly with 1.9. Thank you very much.
     
    David Espada, Sep 21, 2011
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.