setting nil to zero

P

Peña, Botp

Hi,

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

reason is i have quite a number of codes like ff

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

thanks and kind regards -botp
 
M

Mike Stok

Hi,

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

reason is i have quite a number of codes like ff

s.each do |w|
f[w] =3D 0 unless f[w] #i want to kill this lne since it does not =20=
jive with my pseudocode
f[w] +=3D 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 =3D Hash.new(0)
=3D> {}
irb(main):002:0> f[7] +=3D 1
=3D> 1
irb(main):003:0> f
=3D> {7=3D>1}

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

Michael

--=20

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
A

ara.t.howard

Hi,

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

reason is i have quite a number of codes like ff

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

thanks and kind regards -botp

why not

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

??

-a

--=20
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama
 
R

Robert Klemme

Botp said:
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
 

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,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top