# Module/file-scoped "global" variables?

**URL:** <https://www.ruby-forum.com/t/module-file-scoped-global-variables/101181>\
**Category:** Ruby\
**Created:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/module-file-scoped-global-variables/101181 "2006-08-03T14:23:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/module-file-scoped-global-variables/101181/1 "2006-08-03T14:23:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 3, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/module-file-scoped-global-variables/101181/2 "2006-08-03T15:00:52Z")

</div>

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
