Ruby .包括?不';当我添加相同的字符串时,不会引发
我希望能够在运行include之前关闭案例名称。在push中调用它会导致一些奇怪的行为:如果第二个Jack以capitol开头,它会将第二个Jack添加到列表中,但如果第二个Jack是小写的,则不会将其添加到列表中。在包含检查之前,我怎样才能删除案例名称Ruby .包括?不';当我添加相同的字符串时,不会引发,ruby,Ruby,我希望能够在运行include之前关闭案例名称。在push中调用它会导致一些奇怪的行为:如果第二个Jack以capitol开头,它会将第二个Jack添加到列表中,但如果第二个Jack是小写的,则不会将其添加到列表中。在包含检查之前,我怎样才能删除案例名称 $list = [] def hand_out_gift(name) if $list.include?name raise "u can't get more than one present!" else $list
$list = []
def hand_out_gift(name)
if $list.include?name
raise "u can't get more than one present!"
else
$list.push(name.downcase)
puts "Here you go #{name} :)"
end
end
hand_out_gift("Jack")
hand_out_gift("Mary")
hand_out_gift("Jill")
hand_out_gift("Jack")
在您的功能早期,将其关闭:
def hand_out_gift(name)
key = name.downcase
if $list.include? key
raise "u can't get more than one present!"
else
$list.push key
puts "Here you go #{name} :)"
end
end
旁白:避免使用这样的全局变量。我应该在这里使用实例变量吗?