From: Josselin Date: 2007-01-21T00:40:05+09:00 Subject: Re: conditional value in hash array ? On 2007-01-20 16:06:42 +0100, Wolfgang N�dasi-Donner said: > Josselin schrieb: >> hos to have a conditional value ? : :category => domain unless domain.nil? > > It doesn't make sense in this situation, because "domain unless > domain.nil?" evauates to "nil" if "domain" contains "nil". > > If you want to insert "nil" if "domain" is not yet defined use > something like this: > > irb(main):002:0> x = (domain ||= nil) > => nil > irb(main):003:0> a = {:a => (domain ||= nil)} > => {:a=>nil} > >> [ :distance => radius.to_f , :category => domain unless domain.nil?, >> :valid_until => Time.now.utc] >> >> but giving an error.... unexpected kUNLESS_MOD, expecting ']' > > In other situations you should use parenthesis ":category => (domain > unless domain.nil?)". > > It is also possible to use the "?:" operator in some cases: > > irb(main):007:0> a = [:a => (domain.nil??42:84)] > => [{:a=>42}] > > Wolfgang N�dasi-Donner thanks Wolfgang my objective is not entering a value in the array if the domain is nil so it seems I should enter first nil if domain is nil as you stated then compact the array to get rid of nil values..