# Newbie question

**URL:** <https://www.ruby-forum.com/t/newbie-question/189868>\
**Category:** Ruby\
**Created:** [June 9, 2010, 8:45am UTC](https://www.ruby-forum.com/t/newbie-question/189868 "2010-06-09T08:45:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [June 9, 2010, 8:45am UTC](https://www.ruby-forum.com/t/newbie-question/189868/1 "2010-06-09T08:45:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [June 9, 2010, 10:14am UTC](https://www.ruby-forum.com/t/newbie-question/189868/2 "2010-06-09T10:14:21Z")

</div>

On Wed, Jun 9, 2010 at 8:45 AM, lalawawa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [June 9, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/newbie-question/189868/3 "2010-06-09T21:01:49Z")

</div>

2010/6/9 lalawawa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [June 10, 2010, 12:35am UTC](https://www.ruby-forum.com/t/newbie-question/189868/4 "2010-06-10T00:35:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [June 10, 2010, 8:15am UTC](https://www.ruby-forum.com/t/newbie-question/189868/5 "2010-06-10T08:15:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lalawawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@lalawawa](https://www.ruby-forum.com/u/lalawawa)\
**Post date:** [June 11, 2010, 2:47am UTC](https://www.ruby-forum.com/t/newbie-question/189868/6 "2010-06-11T02:47:43Z")

</div>

On Wed, Jun 9, 2010 at 1:45 AM, lalawawa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
