gpt4 book ai didi

ruby - 格式化 "is_a? Integer"的正确方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:04 26 4
gpt4 key购买 nike

我正在尝试使用下面的代码,仅当键是整数时才在散列中保留一个值。我在这里做错了什么?

hackerrank.keep_if do |x,y| x.is_a? Integer end

编辑:我一定是做错了什么,因为我在 HackerRank.com 上不断收到运行时错误。我的完整代码如下:

hackerrank.store[543121,100] # store new key-value pair
hackerrank.keep_if { |x,y| x.is_a?(Integer) } # keep if key is an integer
hackerrank.delete_if { |x,y| x % 2 == 0 } # delete if key is even

编辑 2:挑战链接可在此处获得:https://www.hackerrank.com/challenges/ruby-hash-add-del-sel

最佳答案

它需要看起来像这样(括号,而不是方括号):

hackerrank.store(543121, 100)
#=> 100

关于ruby - 格式化 "is_a? Integer"的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31017890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com