Module/file-scoped "global" variables?

Hello,

I have a file with some functions which share some common variables. I
want to have these functions visible in whole program (or at least in
some classes by making a module from the file and mix it into these
classes), but I’d like to make shared variables remain unvisible to the
rest of the program. Is it possible in Ruby?

Thanks,

P.

On Aug 3, 2006, at 1:05 AM, Pavel S. wrote:

P.

Yeah, if you really want to do this you can do:

module A

shared = 1

define_method(:uses_shared1) { shared += 1; puts shared }
define_method(:uses_shared2) { puts shared }
end

class B
include A
end

b = B.new
b.uses_shared1
b.uses_shared2

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs