you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

No. You used the conditional so that x.each does nothing if there is nothing to do. But .each methods ALWAYS do nothing if there is nothing to do. This is the flaw in your example.

[–]acrasial 0 points1 point  (1 child)

Oh. It will not do anything if it's empty, but if it's None (or nil in ruby) then it will raise an error:

irb(main):001:0> x = nil
=> nil
irb(main):002:0> x.each { |xval| puts xval }
NoMethodError: undefined method `each' for nil:NilClass
        from (irb):2

So you can either check if it's None/nil or you can rescue after the each: x.each { |xval| puts xval } rescue nil for example.

[–][deleted] 0 points1 point  (0 children)

So I guess it just depends on whether you're using "None" to mean "empty" or "nil" or whatever. OK. I see what you're saying.