# Another newbie question

**URL:** <https://www.ruby-forum.com/t/another-newbie-question/124732>\
**Category:** Ruby\
**Created:** [January 1, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/another-newbie-question/124732 "2008-01-01T23:40:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/u/KSSS_sa_MSSSSrton)\
**Post date:** [January 1, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/another-newbie-question/124732/1 "2008-01-01T23:40:49Z")

</div>

Hi group,

I apologize for the newbie question.

Is there in Ruby some possibility for making global  
variables visible from within a DEF, except that  
passing all of them as parameters?

E.g.:

class class1  
…  
def some\_method  
…  
end  
end

…  
g = class1.new  
…

def f(g) \<— i’d like to avoid to pass g here  
…  
g.some\_method  
…  
end

I have not found any other solution in the documentation.  
When the number of these global objects become large (e.g. \>20),  
passing all of them each time becomes incomfortable.  
Any help appreciated.

Marton

---

<div class="post-metadata">

**Author:** ![KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/u/KSSS_sa_MSSSSrton)\
**Post date:** [January 1, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/another-newbie-question/124732/2 "2008-01-01T23:54:16Z")

</div>

Kósa Márton wrote:

> class class1
> 
> Marton

Variables with names that start with $ are global.

$g = class1.new

Bear in mind that using global variables is often considered bad form  
because in large programs it is hard to figure out where the variable is  
used and modified. If your program relies on a lot of global variables  
you might consider re-thinking how your program works.

Whatever you decide to do, good luck!

---

<div class="post-metadata">

**Author:** ![KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/u/KSSS_sa_MSSSSrton)\
**Post date:** [January 2, 2008, 12:20am UTC](https://www.ruby-forum.com/t/another-newbie-question/124732/3 "2008-01-02T00:20:35Z")

</div>

On Jan 1, 10:53 pm, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Whatever you decide to do, good luck!
> 
> –  
> RMagick:[http://rmagick.rubyforge.org/](http://rmagick.rubyforge.org/)  
> RMagick 2:[http://rmagick.rubyforge.org/rmagick2.html](http://rmagick.rubyforge.org/rmagick2.html)

Thank you very much. The $ sign is my friend here.  
I’ll not misuse it.

---

<div class="post-metadata">

**Author:** ![KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/u/KSSS_sa_MSSSSrton)\
**Post date:** [January 2, 2008, 1:20am UTC](https://www.ruby-forum.com/t/another-newbie-question/124732/4 "2008-01-02T01:20:48Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jan 1, 2008, at 5:19 PM, Kósa Márton wrote:

> Thank you very much. The $ sign is my friend here.  
> I’ll not misuse it.

Something else to consider, if the data is related, is to put it into  
a hash and pass that:

config = { :key =\> ‘value’, :foo =\> “bar”, :someNumber =\> “56”,  
‘someClass’ =\> class1.new}

def f(config)

```
config['someClass'].some_method

```

end

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
removed\_email\_address@domain.invalid

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHeti/Uy30ODPkzl0RAn1TAJsEvlsOyqbYW1sA6qyJyszxXbN5TACfVzO9  
E3SUegwXGrgTFMO+bnU8ja4=  
=Pam/  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@KSSS\_sa\_MSSSSrton](https://www.ruby-forum.com/u/KSSS_sa_MSSSSrton)\
**Post date:** [January 2, 2008, 2:47am UTC](https://www.ruby-forum.com/t/another-newbie-question/124732/5 "2008-01-02T02:47:18Z")

</div>

if your program grows to many files, and you still need $ globals,  
you can simply create a file and name it something obvious like  
“globals.rb” and do all of the initializing of the globals there.  
For debugging or error checking you can simply keep track of which  
ruby file was running when the last change was made to the global.

Also consider using instance variables which are prefixed with the @  
sigil. These are visible within anything in that instance object of  
the class. Rails is an example of heavy use of instance variables.  
They can simplify life a lot, but sometimes it is better to pass data  
to a function than to simply use the same instance variable  
everywhere (such as when you want a function or method to use  
encapsulation and do its own validation internally. When you have  
something that interfaces with a lot of other things already, it can  
be a good idea to keep the function/method’s name and signature and  
return the same but retain the ability to change its internals) .
