From: Mike Austin Date: 2010-06-06T05:55:06+09:00 Subject: Re: Processing (potentially) nil method without using nested if On Jun 5, 1:33 pm, Ben Vishny wrote: > Hi all, > > really stupid question. How can I evaluate the following code without > using a nested if.. then statement? > > if parent.respond_to? "children" >     if parent.children.size != 0 >         puts "Has children" >    end > end > > Ideally I'd like to do: > > if parent.respond_to? "children" && parent.children.size != 0 >    puts "Has Children" > end > > But I get an error if parent.children is undefined. Isn't there a way of > stopping if the first condition is false? Hi Ben, Because of precedence rules, your above code is being parsed as: if parent.respond_to?("children" && parent.children.size != 0) puts "Has Children" end "&&" and "and" have different precedence, and when leaving out parenthesis in function calls, it's not as clear as to what's going on (to the reader/writer). In this case, you can surround the two expressions with parenthesis, or use "and" instead. http://blog.jayfields.com/2007/08/ruby-operator-precedence-of-and-which.html Mike > Thanks, > > Vish > -- > Posted viahttp://www.ruby-forum.com/.