2

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.

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.