[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Module/file-scoped "global" variables?

Pavel Smerk

8/3/2006 4:49:00 AM

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.
1 Answer

Logan Capaldo

8/3/2006 12:59:00 PM

0


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

> 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