Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby .包括?不';当我添加相同的字符串时,不会引发_Ruby - Fatal编程技术网

Ruby .包括?不';当我添加相同的字符串时,不会引发

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

我希望能够在运行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.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

旁白:避免使用这样的全局变量。

我应该在这里使用实例变量吗?