Reputation: 681
I have the following code:
class Person
attr_reader :name, :balance
def initialize(name, balance=0)
@name = name
@balance = balance
puts "Hi, #{name}. You have $#{balance}!"
end
end
class Bank
attr_reader :bank_name
def initialize(bank_name)
@bank_name = bank_name
puts "#{bank_name} bank was just created."
end
def open_account(name)
puts "#{name}, thanks for opening an account at #{bank_name}!"
end
end
chase = Bank.new("JP Morgan Chase")
wells_fargo = Bank.new("Wells Fargo")
me = Person.new("Shehzan", 500)
friend1 = Person.new("John", 1000)
chase.open_account(me)
chase.open_account(friend1)
wells_fargo.open_account(me)
wells_fargo.open_account(friend1)
When I call chase.open_account(me)
I get the result Person:0x000001030854e0, thanks for opening an account at JP Morgan Chase!
. I seem to be getting the unique_id (?) and not the name I assigned to @name when I created me = Person.new("Shehzan", 500),
. I've read a lot about class / instance variables and just can't seem to figure it out.
Upvotes: 2
Views: 191
Reputation: 65
You passing an object to the open_account method
You need to do
def open_account(person)
puts "#{person.name}, thanks for opening an account at #{bank_name}!"
end
Upvotes: 0
Reputation: 571
Here you are passing an instance of Person
, not a string.
chase.open_account(me)
You have to either pass me.name
or modify open_account
method to call Person#name
like this
def open_account(person)
puts "#{person.name}, thanks for opening an account at #{bank_name}!"
end
Upvotes: 0
Reputation: 4643
This is because you are passing an instance object assigned to the name variable. You have to do:
def open_account(person)
puts "#{person.name}, thanks for opening an account at #{bank_name}!"
end
Or:
wells_fargo.open_account(friend1.name)
Upvotes: 2