【发布时间】:2016-09-15 13:11:57
【问题描述】:
自学 Ruby,所以请多多包涵。如果我创建一个具有多个定义属性的对象并将该对象推送到一个数组中,我如何在另一种方法中访问其中一个属性以在控制流方案中使用它?我正在制作一个有趣的银行 ATM 程序。我的代码在下面...
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
def initialize
@accounts = []
end
def add_account(account_number, name, balance, pin)
account = AccountMaker.new(account_number, name, balance, pin)
@accounts << account
end
def login_screen(accounts)
def account_number_login(accounts)
puts "Please enter your 7 digit account number."
account_number_input = gets.chomp
puts accounts.instance_variable_get(:account_number)
if (/^\d{7}$/ === account_number_input) and (account_number_input === (what should go here) )
thank_you_msg()
pin_login(account_number_input)
else
error_msg()
account_number_login()
end
end
在此之后我有更多代码,但与问题无关。本质上,我想从帐户数组中提取 :account_number 并在 Login_screen 函数的 if 语句中使用它来查看帐户是否实际存在。任何和所有的帮助将不胜感激。
【问题讨论】:
标签: arrays ruby class object instance-variables