You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, to quickly check if a list contains a value, we use the .contains() builtin.
Example of using .contains()
list = [1, 2, 3]
value = 2
if list.contains(value)
println "${list} contains ${value}"
end
To check if a hash contains a key, we use the .has_key() builtin. Alternatively, we can use the .keys() builtin, and call .contains() on that.
Example of using .has_key()
hash = { "name": "Scott", "website": "https://fuseraft.com" }
key = "name"
# check if hash contains a given key
if hash.has_key(key)
println "${hash} contains ${key}"
end
# it is functionally equivalent to:
if hash.keys().contains(key)
println "${hash} contains ${key}"
end
Perhaps we can introduce a new usage for the in keyword that consolidates these.
Example of proposed in keyword usage
list = [1, 2, 3]
value = 2
if value in list
println "${list} contains ${value}"
end
hash = { "name": "Scott", "website": "https://fuseraft.com" }
key = "name"
if key in hash.keys()
println "${hash} contains ${key}"
end
The text was updated successfully, but these errors were encountered:
Currently, to quickly check if a list contains a value, we use the
.contains()
builtin.Example of using
.contains()
To check if a hash contains a key, we use the
.has_key()
builtin. Alternatively, we can use the.keys()
builtin, and call.contains()
on that.Example of using
.has_key()
Perhaps we can introduce a new usage for the
in
keyword that consolidates these.Example of proposed
in
keyword usageThe text was updated successfully, but these errors were encountered: