Backport from ruby 1.9, including constants

  • Thread starter Victor \Zverok\ Shepelev
  • Start date
V

Victor \Zverok\ Shepelev

Hello all.

During last several monthes I've worked on some library, using the latest
ruby1.9. Now I want to release the library to community, but first I need to
"backport" it.
One small problem I've stumbled upon:

module Constants
TEST = 5
end

class A
end

a = A.new

a.instance_eval{
extend Constants
p TEST #<== here
}

At the "here" string, ruby1.9 had printed "5", but ruby 1.8.5 raises
NameError (uninitialized constant TEST).

I know, the question is silly, but I can't find how to do this. (by some
reasons, the code should affect only one object, not entire class)

Thanks.

V.
 
G

Gary Wright

module Constants
TEST = 5
end

class A
end

a = A.new

a.instance_eval{
extend Constants
p TEST #<== here
}

The only solutions I can think of involve explicitly referencing the
class:

class Object
def singleton_class
(class <<self; self; end)
end
end

a.instance_eval {
extend Constants
singleton_class::TEST
}

But at that point it is probably easier to simply reference Constants
directly:

a.instance_eval {
t = Constants::TEST
}

It does seem a bit strange to be adding constants to the singleton
class.
Why not add them to A itself? That way you don't need to extend the
singleton class with the Constants module.

class A
include Constants
end

A.new.instance_eval {
p self.class::TEST # still a bit ugly...
}


Gary Wright
 
K

Ken Bloom

Hello all.

During last several monthes I've worked on some library, using the latest
ruby1.9. Now I want to release the library to community, but first I need to
"backport" it.
One small problem I've stumbled upon:

module Constants
TEST = 5
end

class A
end

a = A.new

a.instance_eval{
extend Constants
p TEST #<== here
}

At the "here" string, ruby1.9 had printed "5", but ruby 1.8.5 raises
NameError (uninitialized constant TEST).

I know, the question is silly, but I can't find how to do this. (by some
reasons, the code should affect only one object, not entire class)

class << a
extend Constants
p TEST
end
 
J

Joel VanderWerf

Victor said:
Hello all.

During last several monthes I've worked on some library, using the latest
ruby1.9. Now I want to release the library to community, but first I need to
"backport" it.
One small problem I've stumbled upon:

module Constants
TEST = 5
end

class A
end

a = A.new

a.instance_eval{
extend Constants
p TEST #<== here
}

At the "here" string, ruby1.9 had printed "5", but ruby 1.8.5 raises
NameError (uninitialized constant TEST).

module Constants
TEST = 5
end

class A
end

a = A.new

m = Module.new
def m.const_missing k
Constants.const_get(k) || super(k)
end

string_from_file = <<END
TEST
END

test = m.module_eval %{
a.instance_eval {
#{string_from_file}
}
}

p test # ==> 5
 

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,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top