![]() |
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. |
Re: Module/file-scoped "global" variables?
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 |
| All times are GMT. The time now is 12:45 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.