[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

setting nil to zero

Peña, Botp

2/23/2006 2:39:00 AM

Hi,

is there a setting to set nil to zero? something like $NIL=0, then set it back again to $NIL=nil...

reason is i have quite a number of codes like ff

s.each do |w|
f[w] = 0 unless f[w] #i want to kill this lne since it does not jive with my pseudocode
f[w] += 1
end

thanks and kind regards -botp


4 Answers

Mike Stok

2/23/2006 2:48:00 AM

0

On 22-Feb-06, at 9:38 PM, Peña, Botp wrote:

> Hi,
>
> is there a setting to set nil to zero? something like $NIL=0, then
> set it back again to $NIL=nil...
>
> reason is i have quite a number of codes like ff
>
> s.each do |w|
> f[w] = 0 unless f[w] #i want to kill this lne since it does not
> jive with my pseudocode
> f[w] += 1
> end
>
> thanks and kind regards -botp

If I'm doing that with a hash then I use 0 as a default value e.g.

michael-stoks-powerbook-g4-17:~ mike$ irb
irb(main):001:0> f = Hash.new(0)
=> {}
irb(main):002:0> f[7] += 1
=> 1
irb(main):003:0> f
=> {7=>1}

but I assume that you are after something more subtle...

Michael

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.






Ara.T.Howard

2/23/2006 2:51:00 AM

0

Robert Klemme

2/23/2006 9:16:00 AM

0

Botp wrote:
> Hi,
>
> is there a setting to set nil to zero? something like $NIL=0, then
> set it back again to $NIL=nil...
>
> reason is i have quite a number of codes like ff
>
> s.each do |w|
> f[w] = 0 unless f[w] #i want to kill this lne since it does not
> jive with my pseudocode
> f[w] += 1
> end

.... and if f is not a Hash you can do

f[w] = (f[w] || 0) + 1

Kind regards

robert

Dave Burt

2/23/2006 9:50:00 AM

0

Robert Klemme wrote:
> Botp wrote:
>> f[w] += 1
>> end
>
> .... and if f is not a Hash you can do
>
> f[w] = (f[w] || 0) + 1
>

You want an integer from Mr. Nil? Just ask him.

f[w] = f[w].to_i + 1

Cheers,
Dave

--
Posted via http://www.ruby-....