From: Robert Klemme Date: 2010-06-10T15:15:08+09:00 Subject: Re: newbie question Please do not top post. On 10.06.2010 00:33, lalawawa wrote: > 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. You can easily test this out for yourself: $ ruby19 < haveACat=true > myLongName = 'woof' > if haveACat > myLoongName += ' and meow' > end > puts myLongName > XXX -:4:in `
': 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/