# Understanding Variable Naming in Ruby

**URL:** <https://www.ruby-forum.com/t/understanding-variable-naming-in-ruby/81897>\
**Category:** Ruby\
**Created:** [March 14, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/understanding-variable-naming-in-ruby/81897 "2006-03-14T21:24:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 14, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/understanding-variable-naming-in-ruby/81897/1 "2006-03-14T21:24:59Z")

</div>

I’m trying to better understand how variables are named in Ruby…  
here’s what I think I understand. Please correct me where I  
misunderstand:

#Globals are defined outside a class/method and are  
#available everywhere. Must starts with ‘$’  
$global\_variable

#Class names must start with capitalized letter.  
class Class\_name

```
 #Starts with '@@' and is available to all methods in the class.
 @@class_variable

 #Method name must start with lowercase letter or '_'
 def method_name

     #Must start with '@' similar to Python's self notation.
     #This variable is local to the method.
     @method_variable

 end

```

end

Where do constants and local variables fit into this?

Thanks,  
Brad

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 14, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/understanding-variable-naming-in-ruby/81897/2 "2006-03-14T21:34:10Z")

</div>

Brad wrote:

> Where do constants and local variables fit into this?

Constants typically are in all upper case characters. Then local  
variables don’t start with a leading @@, @, or $. Something like  
thisLocalVar or this\_local\_var depending on your background 🙂

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 14, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/understanding-variable-naming-in-ruby/81897/3 "2006-03-14T22:01:09Z")

</div>

Here is a nonsensical class that uses class, instance, and local  
variables. I don’t think creating my own class variable accessors is  
Rubyish, but I just threw it in there for kicks.

class Foo  
attr\_accessor :instVar, :localVar  
@@classVar=“class variable”  
localVar=“local variable”

```
def initialize
	super
	@instVar="instance variable"
end

def self.classVar
	return @@classVar
end

def self.classVar=other
	@@classVar=other
	return @@classVar
end

def localVarCheck
	begin
		print "#{self}'s local variable is #{localVar}.\n"
	rescue NameError => e
		print "#{self}'s local variable doesn't exist! The specific error

```

is:\n#{e}.\n"  
end  
end

end

print “Foo’s classVar is set to #{Foo.classVar}.\n”  
print “Foo’s classVar is now changed to #{Foo.classVar=“new class  
variable”}.\n”  
foo=Foo.new  
print “foo’s instVar is set to #{foo.instVar}.\n”  
print “foo’s instVar is now changed to #{foo.instVar=“new instance  
variable”}.\n”  
foo.localVarCheck
