Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Module/file-scoped "global" variables? (http://www.velocityreviews.com/forums/t832036-module-file-scoped-global-variables.html)

Pavel Smerk 08-03-2006 04:49 AM

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.

Logan Capaldo 08-03-2006 12:59 PM

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 08:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.