四面觀望,無法找到正確答案來解決我面臨的問題。我想訪問並檢查一個哈希值內的值。代碼如下...訪問並檢查散列內的值
class Bank
class AccountMaker
attr_accessor :account_number, :name, :balance, :pin
def initialize(account_number, name, balance, pin)
@account_number = account_number
@name = name
@balance = balance
@pin = pin
end
end
attr_accessor :int_account_number, :int_account_pin
def initialize
@accounts = {}
@int_account_number = int_account_number
@int_account_pin = int_account_pin
end
def add_account(account_number, name, balance, pin)
@accounts[account_number] = AccountMaker.new(account_number, name, balance, pin)
end
def login_screen
def account_number_login
puts "Please enter your 7 digit account number."
account_number = gets.chomp
int_account_number = account_number.to_i
if @accounts.has_key?(int_account_number) and (/^\w{7}$/ === account_number)
thank_you_msg()
pin_login(int_account_number)
else
error_msg()
account_number_login()
end
end
def pin_login(int_account_number)
puts "Please enter your 4 digit pin."
account_pin = gets.chomp
int_account_pin = account_pin.to_i #May have to use this later
#puts int_account_number, int_account_pin #Used to check if variables come through
if (What Should go here?) == int_account_pin #(/^\d{4}$/ === account_pin)
thank_you_msg
main_menu()
else
error_msg()
pin_login(int_account_number)
end
end
account_number_login()
end
def main_menu
end
end
我的問題是如何訪問針的哈希值用戶inputed賬號(鍵)並檢查它是否在用戶輸入相匹配?哈希值包含在pin下的值,但我在嘗試訪問和比較時遇到了最困難的時間。
真棒我欣賞的幫助和所有的信息。如果它不明顯,我是新來的Ruby。再次感謝 – JCD