Hash.includes?给人以奇怪的结果

Nic*_*ell 3 crystal-lang

我正在尝试编写这个Python代码的Crystal等价物:

test_hash = {}
test_hash[1] = 2
print(1 in test_hash)
Run Code Online (Sandbox Code Playgroud)

这打印为True,因为1是dict的键之一.

这是我尝试过的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)
Run Code Online (Sandbox Code Playgroud)

includes?在这里返回false.为什么?什么是我的Python代码的正确等价物?

Nic*_*ell 5

has_key?改用.has_key?问哈希是否有那个密钥.

但是,includes?检查某个键/值对是否在哈希表中.如果只提供密钥,它将始终返回false.

例:

# 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.has_key?(1)
# Check if the Hash includes 1 => 2
pp! test_hash.includes?({1, 2})


# Pointless, do not use
pp! test_hash.includes?(1)
Run Code Online (Sandbox Code Playgroud)