Skip to content

Instantly share code, notes, and snippets.

@perfectfoolish
Created July 17, 2013 08:27
Show Gist options
  • Save perfectfoolish/6018802 to your computer and use it in GitHub Desktop.
Save perfectfoolish/6018802 to your computer and use it in GitHub Desktop.
class Account
attr_reader :name, :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
def display_balance(pin_number)
puts pin_number == pin ? "Balance: $#{@balance}." : pin_error
end
def withdraw(pin_number, amount)
if pin_number == pin
@balance -= amount
puts "Withdrew #{amount}. New balance: $#{@balance}."
else
puts pin_error
end
end
private
def pin
@pin = 1234
end
def pin_error
"Access denied: incorrect PIN."
end
end
my_account = Account.new("Eric", 1_000_000)
my_account.withdraw(11, 500_000)
my_account.display_balance(1234)
my_account.withdraw(1234, 500_000)
my_account.display_balance(1234)
@perfectfoolish
Copy link
Author

my_account.display_balance(1234)执行的结果是:Balance: $1000000. 那个display_balance 方法中的$的作用是? @happypeter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment