Module/file-scoped "global" variables?

P

Pavel Smerk

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

Logan Capaldo

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.

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top