Setting nil to zero

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

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

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 S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

On Thu, 23 Feb 2006, [iso-8859-1] Peña, Botp wrote:

thanks and kind regards -botp

why not

f = Hash::new{|h,k| h[k] = 0}
s.each{|w| f[w] += 1}

??

-a

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

f[w] += 1
end

thanks and kind regards -botp

s.each do |w|
f[w] = f[w].to_i + 1 # Or if f[w] can be a float, f[w].to_f
end

Botp wrote:

f[w] += 1
end

… and if f is not a Hash you can do

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

Kind regards

robert

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