From: Shadowfirebird Date: 2008-08-13T01:21:18+09:00 Subject: Re: What's the easiest way to check if param[:x][:y] exists? Sorry, you're right: irb(main):001:0> a = {:a=>{:x=>1}} => {:a=>{:x=>1}} irb(main):003:0> p a[:a][:x] 1 => nil irb(main):004:0> p a[:a][:y] nil => nil irb(main):005:0> p a[:b][:x] NoMethodError: undefined method `[]' for nil:NilClass from (irb):5 David's *much* smarter than me, so listen to him... On Tue, Aug 12, 2008 at 5:18 PM, Shadowfirebird wrote: > It looks to me as if you should be able to check for nil. > > foo( param[:x][:y] ) unless (param[:x][:y] == nil) > > On Tue, Aug 12, 2008 at 5:03 PM, Joshua Muheim wrote: >> Hi all >> >> In PHP I can simply check whether $param['x']['y'] exists regardless >> whether $param['x'] exists or not. >> In Rails I get a NoMethodError when using >> >> if param[:x][:y] >> .. >> end >> >> when param[:x] is nil. So I always have to use >> >> if param[:x] and param[:x][:y] >> .. >> end >> >> but I think that's not very beautiful - think about a case where I have >> to check whether param[:a][:b][:c][:d][:...] exists or not! >> >> Maybe there's a cooler way to do this? ;-) >> >> Thanks >> Josh >> -- >> Posted via http://www.ruby-forum.com/. >> >> > > > > -- > All you can do is try to know who your friends are as you head off to > the war / Pick a star on the dark horizon and follow the light > -- All you can do is try to know who your friends are as you head off to the war / Pick a star on the dark horizon and follow the light