conditional value in hash array ?

J

Josselin

hos to have a conditional value ? : :category => domain unless domain.nil?

wrote :

[ :distance => radius.to_f , :category => domain unless domain.nil?,
:valid_until => Time.now.utc]

but giving an error.... unexpected kUNLESS_MOD, expecting ']'
 
W

Wolfgang Nádasi-Donner

Josselin said:
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
 
J

Josselin

Josselin said:
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..
 
R

Robert Klemme

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..

What exactly are you trying to achieve? Is it maybe rather something
like this:

hash = {
:distance => radius.to_f,
:valid_until => Time.now.utc
}
hash[ :category ]= domain unless domain.nil?

Regards

robert
 
J

Josselin

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..

What exactly are you trying to achieve? Is it maybe rather something
like this:

hash = {
:distance => radius.to_f,
:valid_until => Time.now.utc
}
hash[ :category ]= domain unless domain.nil?

Regards

robert

thanks that's better... forgot the key nme ..
 
G

Gennady Bystritsky

------_=_NextPart_001_01C73CDB.C35D2DB7
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

This should work:

[ :distance =3D> radius.to_f , :category =3D> (domain unless =
domain.nil?),=20
:valid_until =3D> Time.now.utc]

Gennady.


-----Original Message-----
From: Josselin [mailto:[email protected]]
Sent: Sat 1/20/2007 6:05
To: ruby-talk ML
Subject: conditional value in hash array ?
=20
hos to have a conditional value ? : :category =3D> domain unless =
domain.nil?

wrote :

[ :distance =3D> radius.to_f , :category =3D> domain unless domain.nil?, =

:valid_until =3D> Time.now.utc]

but giving an error.... unexpected kUNLESS_MOD, expecting ']'




------_=_NextPart_001_01C73CDB.C35D2DB7--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,757
Latest member
PDIJaclyn

Latest Threads

Top