Method to determine if hash has a specific key

Asked 2 years ago, Updated 2 years ago, 98 views

While continuing to add a key according to the input, the existing key is key0, key1,... So I'm going to add a number to the back.

I can do the part that adds a number to the back, but I can't find a method to check whether a specific key exists or not.

hashcode ruby

2022-09-22 22:10

1 Answers

Write key?.

Returns true if the given key is present in hsh.

Returns true if hsh has key.

h = { "a" => 100, "b" => 200 }
h.has_key?("a")   #=> true
h.has_key?("z")   #=> false

has_key? that does the same thing, but is not recommended.

[Related materials ]


2022-09-22 22:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.