# Declaring variables?

**URL:** <https://rubytalk.org/t/declaring-variables/64>\
**Category:** ruby-talk\
**Created:** [3 June 2002 23:34 UTC](https://rubytalk.org/t/declaring-variables/64 "2002-06-03T23:34:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_Mak2](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@Philip\_Mak2](https://rubytalk.org/u/Philip_Mak2)\
**Post date:** [3 June 2002 23:34 UTC](https://rubytalk.org/t/declaring-variables/64/1 "2002-06-03T23:34:20Z")

</div>

As I understand it, there isn’t really a concept of “declaring” a  
variable in ruby. When I make an assignment to a variable, it’s  
automatically created with the type of the rvalue if needed.

If I try to read a variable that doesn’t exist, I’ll get an exception.

But what if I try to set the wrong variable name? e.g.:

number = 3  
numbr += 1 # a typo  
puts number # prints 3, but should have printed 4

Is there a way to protect myself from typos on lvalue variable names?

---

<div class="post-metadata">

**Author:** ![Albert2](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Albert2](https://rubytalk.org/u/Albert2)\
**Post date:** [4 June 2002 00:33 UTC](https://rubytalk.org/t/declaring-variables/64/2 "2002-06-04T00:33:02Z")

</div>

no

> **···**
>
> On Monday 03 June 2002 06:34 pm, you wrote:
> 
> > As I understand it, there isn’t really a concept of “declaring” a  
> > variable in ruby. When I make an assignment to a variable, it’s  
> > automatically created with the type of the rvalue if needed.
> > 
> > If I try to read a variable that doesn’t exist, I’ll get an exception.
> > 
> > But what if I try to set the wrong variable name? e.g.:
> > 
> > number = 3  
> > numbr += 1 # a typo  
> > puts number # prints 3, but should have printed 4
> > 
> > Is there a way to protect myself from typos on lvalue variable names?
> 
> –  
> Quantum Mechanics: The dreams stuff is made of

---

<div class="post-metadata">

**Author:** ![Chris\_Morris1](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@Chris\_Morris1](https://rubytalk.org/u/Chris_Morris1)\
**Post date:** [4 June 2002 01:37 UTC](https://rubytalk.org/t/declaring-variables/64/3 "2002-06-04T01:37:21Z")

</div>

> number = 3  
> numbr += 1 # a typo  
> puts number # prints 3, but should have printed 4
> 
> Is there a way to protect myself from typos on lvalue variable names?

Well, literally in that case, you’d get this error:

irb(main):001:0\> numbr += 1  
NameError: undefined method `+’ for nil  
from (irb):1

Now, if numbr had been used previously, you’d still have your original  
issue – there’s no declaring of variables (like VB’s Option Explicit) to  
protect you.

One Best Practice that seems to cover nicely for this sort of thing is unit  
testing. The benefits of unit testing go beyond preventing simple mistakes  
like this one. It is sort of a non-answer to your question, but I’ve rarely  
run into problems with the dynamic typing and declaration-free attributes of  
Ruby with unit testing.

Chris
