Forum: Ruby Q: variable "declarations"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ilja Tabachnik (Guest)
on 2006-03-17 09:54
(Received via mailing list)
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.
Farrel L. (Guest)
on 2006-03-17 13:03
(Received via mailing list)
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/
Jenda K. (Guest)
on 2007-02-28 15:34
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.
Daniel B. (Guest)
on 2007-02-28 19:50
(Received via mailing list)
On Feb 28, 6:34 am, Jenda K. <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
Neville F. (Guest)
on 2007-03-01 01:12
Daniel B. wrote:
> On Feb 28, 6:34 am, Jenda K. <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-c...
This topic is locked and can not be replied to.