Peña, Botp
2/23/2006 10:19:00 AM
Robert Klemme [mailto:bob.news@gmx.net] :
#f[w] = (f[w] || 0) + 1
clear rubyish solution, Robert.
thanks again for generous help.
i am still hoping for the plain f[w] += 1 though :)
for the record, i played w the ff
ruby>cat test.rb
class NilClass
def method_missing(methId, *args)
str = methId.id2name
if str == "+" and args[1].nil?
#str is "+"
#args[1] is nil
#args[0] is rhs value
return args[0]
end
end
end
f = {}
f["test"] += 1
f["test2"] += 2
p f
f = []
f[1] += 1
f[5] += 2
p f
ruby>ruby test.rb
{"test2"=>2, "test"=>1}
[nil, 1, nil, nil, nil, 2]
ruby>
I doubted if it's robust enough, so my q...
-botp
#Kind regards
#
# robert
#
#
#