In perl, one is able to sort of ‘declare’ variables by mentioning them
in ‘my’ statements. Then, if you say ‘use strict;’, no variables that
weren’t previously thus ‘declared’ are allowed. This is useful in that
it catches typos that might be difficult to detect in testing.
Does ruby have an equivalent mechanism?
On Wed, Jun 9, 2010 at 8:45 AM, lalawawa [email protected] wrote:
In perl, one is able to sort of ‘declare’ variables by mentioning them
in ‘my’ statements. Then, if you say ‘use strict;’, no variables that
weren’t previously thus ‘declared’ are allowed. This is useful in that
it catches typos that might be difficult to detect in testing.
Does ruby have an equivalent mechanism?
Well one can say, that concerning local variables the “my” is
implicit. Instance variables spring into life without “declaration”.
In Ruby 1.9 this gives you a warning though.
HTH
R.
2010/6/9 lalawawa [email protected]:
In perl, one is able to sort of ‘declare’ variables by mentioning them
in ‘my’ statements. Then, if you say ‘use strict;’, no variables that
weren’t previously thus ‘declared’ are allowed. This is useful in that
it catches typos that might be difficult to detect in testing.
Does ruby have an equivalent mechanism?
Better: you do not need an additional declaration, rather Ruby will
catch situations for you where you forgot to initialize (aka assign) a
local variable:
$ ruby19 -e ‘puts foo’
-e:1:in <main>': undefined local variable or method
foo’ for
main:Object (NameError)
The grain of salt is this
-
this does not work for instance variables, these spring into
existence whenever you use them,
-
because there is the method local variable ambiguity you might still
run into erroneous situations when forgetting to initialize a local
variable which shares name with a method.
Kind regards
robert
Hi Robert,
I assume that if I use the value of an unassigned variable ruby will
catch me. What I am worried about is if I misspell a variable name
while assigning to it.
myLongName = ‘woof’
if haveACat
myLoongName += ’ and meow’
end
puts myLongName
I misspelled ‘myLongName’ the second time, so it’s like the assinment
didn’t happen. If I had been in perl and said ‘my myLongName’ and ‘use
strict’, it would have caught the first time I ran it, even if I didn’t
test the case where ‘haveACat’ is true.
Please do not top post.
On 10.06.2010 00:33, lalawawa wrote:
I misspelled ‘myLongName’ the second time, so it’s like the assinment
didn’t happen. If I had been in perl and said ‘my myLongName’ and ‘use
strict’, it would have caught the first time I ran it, even if I didn’t
test the case where ‘haveACat’ is true.
You can easily test this out for yourself:
$ ruby19 <<XXX
haveACat=true
myLongName = ‘woof’
if haveACat
myLoongName += ’ and meow’
end
puts myLongName
XXX
-:4:in <main>': undefined method
+’ for nil:NilClass (NoMethodError)
nil does not have many methods so it’s likely that the error will
surface that way.
Also, writing unit tests or rspecs will catch behavioral errors. I
cannot recall many spelling errors that I made in Ruby programs and I
can’t remember a single one that went unnoticed. So while I agree that
Perl seems to provide more safety here for me the practical benefit is
small.
This goes a bit in a similar direction as the discussion about
dynamically typed scripting languages being unsafe or not suited for
large scale applications because they lack type safety of statically
typed languages. In theory that all sounds good but it turns out that
in practice there are not many issues that would have been caught by
static type checking. Other classes of bugs seem more prevalent - in my
experience.
Cheers
robert
On Wed, Jun 9, 2010 at 1:45 AM, lalawawa [email protected] wrote:
In perl, one is able to sort of ‘declare’ variables by mentioning them
in ‘my’ statements. Then, if you say ‘use strict;’, no variables that
weren’t previously thus ‘declared’ are allowed. This is useful in that
it catches typos that might be difficult to detect in testing.
Does ruby have an equivalent mechanism?
You can also get an ide / editor that supports auto completion. There is
a
link to a giant google spreadsheet running around here somewhere. You
might
try looking into that if you are concerned.