Reputation: 79
Why does this:
elements = Hash.new()
elements[100] = "a"
elements[200] = "b"
elements[300] = "c"
elements[400] = "d"
print "Count: ", elements.count(),
elements.delete(100)
print "Count: ", elements.count(),
return this:
Count: 4
Count: 3
I wonder why that wouldn't return anything except the value 100
.
Upvotes: 3
Views: 79
Reputation: 34328
This is working as expected.
You are printing the count for your elements
hash which is 4
at the beginning, then you are deleing one element using: elements.delete(100)
then printing the count again, which is 3
now.
See this way to understand what's going on with your elements
hash:
elements = Hash.new()
elements[100] = "a"
elements[200] = "b"
elements[300] = "c"
elements[400] = "d"
puts "elements: #{elements.inspect}"
puts "Count: #{elements.count()}"
elements.delete(100)
puts "elements: #{elements.inspect}"
puts "Count: #{elements.count()}"
# > elements: {100=>"a", 200=>"b", 300=>"c", 400=>"d"}
# > Count: 4
# > elements: {200=>"b", 300=>"c", 400=>"d"}
# > Count: 3
Upvotes: 3