# Q: variable "declarations"

**URL:** <https://www.ruby-forum.com/t/q-variable-declarations/55141>\
**Category:** Ruby\
**Created:** [March 17, 2006, 8:54am UTC](https://www.ruby-forum.com/t/q-variable-declarations/55141 "2006-03-17T08:54:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilja\_Tabachnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@Ilja\_Tabachnik](https://www.ruby-forum.com/u/Ilja_Tabachnik)\
**Post date:** [March 17, 2006, 8:54am UTC](https://www.ruby-forum.com/t/q-variable-declarations/55141/1 "2006-03-17T08:54:19Z")

</div>

Hi All !

I’m new to Ruby so my question could look a bit… hmmm… stupied.  
However I do did a search in comp.lang.ruby archieves and  
found a lot of questions similar to the my one - but no definite  
answer. Reading Programming Ruby also didn’t help.

I really don’t understand how one could find errors (typos) in  
variable names, like in this example:

def do\_something(arg)

result = 1

# some code…

if some\_condition  
reslt = 2 # a typo here!!!  
endif

# more code …

result  
end

The code above is OK for Ruby - even no warnings. I could spend  
hours or even days finding out why the damn thing doesn’t work!  
(given there are many thousands lines of code)

So I’d like to ask - is there really no way do somehow “declare”  
local variable (like “use strict” and “my” in Perl)? If no, what’s  
“The Right Way” to deal with such situations in Ruby?

Many thanks in advance,

Ilja.

---

<div class="post-metadata">

**Author:** ![Ilja\_Tabachnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@Ilja\_Tabachnik](https://www.ruby-forum.com/u/Ilja_Tabachnik)\
**Post date:** [March 17, 2006, 12:03pm UTC](https://www.ruby-forum.com/t/q-variable-declarations/55141/2 "2006-03-17T12:03:43Z")

</div>

I would suggest unit tests using the Test::Unit API which comes  
standard with ruby these days.

C:\>more test.rb  
require ‘test/unit’

def do\_something  
result = 1

# some code…

reslt = 2 # a typo here!!!  
result  
end

class MyTestCase \< Test::Unit::TestCase  
def test\_do\_something  
assert\_equal(2,do\_something)  
end  
end

C:\>ruby test.rb  
Loaded suite test  
Started  
F  
Finished in 0.031 seconds.

1. Failure:  
test\_do\_something(MyTestCase) [test.rb:12]:  
\<2\> expected but was  
\<1\>.

1 tests, 1 assertions, 1 failures, 0 errors

More info at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/)

---

<div class="post-metadata">

**Author:** ![Ilja\_Tabachnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@Ilja\_Tabachnik](https://www.ruby-forum.com/u/Ilja_Tabachnik)\
**Post date:** [February 28, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/q-variable-declarations/55141/3 "2007-02-28T14:34:08Z")

</div>

Farrel L. wrote:

> I would suggest unit tests using the Test::Unit API which comes  
> standard with ruby these days.

I would suggest using a programming language.

---

<div class="post-metadata">

**Author:** ![Ilja\_Tabachnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@Ilja\_Tabachnik](https://www.ruby-forum.com/u/Ilja_Tabachnik)\
**Post date:** [March 1, 2007, 12:12am UTC](https://www.ruby-forum.com/t/q-variable-declarations/55141/4 "2007-03-01T00:12:01Z")

</div>

Daniel B. wrote:

> On Feb 28, 6:34 am, Jenda K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Farrel L. wrote:
> > 
> > > I would suggest unit tests using the Test::Unit API which comes  
> > > standard with ruby these days.
> > 
> > I would suggest using a programming language.
> 
> That seems like a rather snarky reply, coming nearly a year (!) after  
> the original post.
> 
> I assume this is the Jenda K. of ActiveState/Perl fame?
> 
> Dan

I touched on the issue of lack of variable declarations in my short  
crtique here:  
[http://blog.surfulater.com/2007/02/21/write-ruby-code-faster-with-ed-for-windows/](http://blog.surfulater.com/2007/02/21/write-ruby-code-faster-with-ed-for-windows/)

---

<div class="post-metadata">

**Author:** ![Ilja\_Tabachnik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@Ilja\_Tabachnik](https://www.ruby-forum.com/u/Ilja_Tabachnik)\
**Post date:** [February 28, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/q-variable-declarations/55141/5 "2007-02-28T18:50:35Z")

</div>

On Feb 28, 6:34 am, Jenda K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Farrel L. wrote:
> 
> > I would suggest unit tests using the Test::Unit API which comes  
> > standard with ruby these days.
> 
> I would suggest using a programming language.

That seems like a rather snarky reply, coming nearly a year (!) after  
the original post.

I assume this is the Jenda K. of ActiveState/Perl fame?

Dan
