Constant domain and procs

D

David Espada

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.
 
R

Robert Klemme

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

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
 
D

David Espada

El miércoles 21 de septiembre, Robert Klemme escribió:
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.

I have tested it and works perfectly with 1.9. Thank you very much.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top