Q: variable "declarations"

I

Ilja Tabachnik

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.
 
F

Farrel Lifson

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 =3D 1
# some code...
reslt =3D 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/
 
J

Jenda Krynicky

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

I would suggest using a programming language.
 
D

Daniel Berger

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 Krynicky of ActiveState/Perl fame?

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top