From: Farrel Lifson Date: 2006-11-01T21:39:26+09:00 Subject: Re: ruby programming best practice On 01/11/06, Shannon Fang wrote: > As a dynamic language, Ruby is much more flexible and easier than other > language I use, for example, Delphi. However, there is a "intrinsic" problem > in ruby programming -- a typical error I get again and again: > > private method `split' called for nil:NilClass > > The following statement: > > array.pop.split(',') > > will generate this error if array is empty. Of course I can use rescue > Exception statement, as a matter of fact, I do use that. Even after 4-5 > years, some of my program still generate these kind of error occasionally. > > Real-world situation is just too complicated. How do we write robust AND > elegant code, and let our thinking smooth rather than considering exceptions > all the time? > > Experts, please tell us, your best practice, facing this "intrinsic" problem > of dynamic languages... > > Thanks, > Shannon > > _________________________________________________________________ > Be the first to hear what's new at MSN - sign up to our free newsletters! > http://www.msn.co.uk/newsletters There's a number of ways to handle this. If the array is possibly empty you could check beforehand and return a default result array.empty? ? ['','',''] : array.pop.split(',') If the code that handles the split result can handle empty arrays then you could just array.pop.to_s.split(',') or String(array.pop).split(',') Farrel