0

我试图写一个简单的用户名/密码提示。我仍然是 Ruby 的初学者。

    combo = Hash.new
combo["placidlake234"] = "tastychicken"
combo["xxxdarkmasterxxx"] = "pieisgood"
combo["dvpshared"] = "ilikepie"

puts "Enter your username."
username = gets.chomp

def user_check
  if username = ["placidlake234"||"xxxdarkmasterxxx"||"dvpshared"]
    puts "What is your password?"
    password = gets.chomp
    pass_check
  else
    puts "Your username is incorrect."
  end
end

def pass_check
  if password => username
    puts "You have signed into #{username}'s account."
  end
end

user_check()

当我尝试运行它时,我在 username in 之前收到一个奇怪的错误=> username

4

1 回答 1

0

有几件事需要纠正:
我在下面评论了

combo = Hash.new
combo["placidlake234"] = "tastychicken"
combo["xxxdarkmasterxxx"] = "pieisgood"
combo["dvpshared"] = "ilikepie"

puts "Enter your username."
username = gets.chomp

def user_check(username, combo)
  #HERE combo.keys gives keys. 
  if combo.keys.include? username
    puts "What is your password?"
    password = gets.chomp
    if pass_check(username, password, combo)
      puts "You have signed into #{username}'s account." 
    else
      puts "Wrong password, sorrie"
    end
  else
    puts "Your username is incorrect."
  end
end

def pass_check(username, password, combo)
  #Here, access by combo[username]
  return true if password == combo[username]
  false
end

#HERE, pass the arguments, so that it is available in function scope
user_check(username, combo)
于 2013-10-20T17:24:50.080 回答