[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

conditional value in hash array ?

Josselin

1/20/2007 2:01:00 PM

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 ']'

6 Answers

WoNáDo

1/20/2007 3:07:00 PM

0

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



Josselin

1/20/2007 3:38:00 PM

0

On 2007-01-20 16:06:42 +0100, Wolfgang Nádasi-Donner
<wonado@donnerweb.de> 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..


Robert Klemme

1/20/2007 4:57:00 PM

0

On 20.01.2007 16:38, Josselin wrote:
> 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

Josselin

1/20/2007 5:55:00 PM

0

On 2007-01-20 17:57:01 +0100, Robert Klemme <shortcutter@googlemail.com> said:

> On 20.01.2007 16:38, Josselin wrote:
>> 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 ..

Gennady Bystritsky

1/20/2007 9:42:00 PM

0

This should work:

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

Gennady.


-----Original Message-----
From: Josselin [mailto:josselin@wanadoo.fr]
Sent: Sat 1/20/2007 6:05
To: ruby-talk ML
Subject: conditional value in hash array ?

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 ']'



The Revd

12/6/2012 9:33:00 PM

0

On Thu, 6 Dec 2012 21:05:45 +0000 (UTC), SmallHernia aka NoSpamAtAll
aka HQ <hq-observer@xxx.yyy> wrote:

>In article <5n12c89644bm80uh6ntrk6pc841pp1rhmb@4ax.com>,
>The Revd <peeling@degenerate.Grik> wrote:
>
>> On Thu, 6 Dec 2012 17:23:07 +0000 (UTC), SmallHernia aka NoSpamAtAll,
>> forgetting he's supposed to be "HQ" <spamnot@not.home> wrote:
>
> ??
>
>>> Yes, *I* made the assertion first, Sir.
>
>Forgery; I didn't write this. Be careful with these
>forgeries -- you left a double space where there
>should not be one.

Forgery? LOL!!! Ironic, coming from the most prolific forger on
Usenet!

>>> Only to those like me who cannot add them.
>
>Forgery; I didn't write this.

See above, jewboi.

>>> Well? Let's start with an easy one: 113 + 892. Can you do it?
>
>> WE aks the questions, jewboi.
>
>Ok... how about 17 + 89?

WE aks ALL the questions, jewboi.