Ruby needs "my" for variable declaration and scoping

I’m still digging Ruby as I plug along over the weekends working on writing a Bliki as an exercise to learn Ruby and Rails.

One lesson many of us learned in the Perl days that seems not to have made it into Ruby was doing a “use strict” to require variable declaration. The biggest benefit being that it required you to declare and scope your variables before you could use them, especially critical in a mod_perl environment. This IMHO is a “very good thing” and saves a lot of headache and troubleshooting to find minor typos.

That said, Ruby does scope your variables nicely for you and is smarter than Perl was about it when not running strict, however, I totally agree with this quote from RubyGarden:


“One thing Perl’s my() gives you is a guarantee that you aren’t stomping on a given variable. For example, if I’m in a block of code in Perl and want to use a private variable I can just declare it with my() and never have to worry about whether a variable of that name existed previously. In Ruby, if you are inside of a block and want a private variable, you need to choose a variable name that has never been used before. I don’t want to have to hold the entire symbol table in my head!! What can I do?”

No language is perfect and just like Java has it’s flaws for my tastes, I’m finding Ruby does also. I wouldn’t be as excited about Ruby as a language if it weren’t for rails, gems, and rake but rails really is what puts it over the edge for me to make it a winning web development platform.

This entry was posted in Ruby. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *