我正在尝试编写与此Python代码等效的Crystal代码:
但是在这里,
test_hash = {}
test_hash[1] = 2
print(1 in test_hash)
这段代码会输出True,因为1是字典中的一个键。
下面是我尝试过的Crystal代码:
# Create new Hash
test_hash = Hash(Int32, Int32).new
# Map 1 to 2
test_hash[1] = 2
# Check if the Hash includes 1
pp! test_hash.includes?(1)
但是在这里,
includes?
返回false。为什么?我的Python代码的正确等价物是什么?
includes?
?这里感觉有些不对劲... - rogerdpackincludes?
"并不重要。includes?({K,V})
是因为Hash
是Enumerable<{K,V}>
的结果。 Hash能够被枚举是非常棒的事情,即使我从未使用过Hash#includes?
,我仍可能使用Hash#each
,Hash#group_by
或其他任何出色的工具。 - Amadan