0
class BankAccount

  def self.create_for(first_name, last_name)
    @accounts ||= []
    @accounts << BankAccount.new(first_name, last_name)
  end

  def initialize(first_name, last_name)
    @balance = 0
    @first_name = first_name
    @last_name = last_name
  end

 def self.find_for(first_name, last_name)
    @accounts.find{|account| account.full_name == "#{first_name} #{last_name}"}
  end

  def full_name
    "#{@first_name} #{@last_name}"
  end

end

How does the method self.find_for works?. I am getting confused on how the account variable has access to full_name method?.

bankacc = BankAccount.create_for "Kevin", "Shane"
BankAccount.find_for("Kevin", "Shane")
puts bankacc.inspect
1
  • 1
    Why does that confuse you? full_name is a simple instance method and account is an instance of BankAccount, so you can call the instance mehtod full_name on it. Commented Mar 31, 2014 at 14:10

2 Answers 2

2

Maybe it helps you to understand the concept if you split the file into two.

This class creates and keeps track of BankAccount instances:

class BankAccounts
 def self.create_for(first_name, last_name)
   @accounts ||= []
   @accounts << BankAccount.new(first_name, last_name)
 end

 def self.find_for(first_name, last_name)
   @accounts.find{|account| account.full_name == "#{first_name} #{last_name}"}
 end
end

This is just a dumb class which you can create instances of:

class BankAccount
  def initialize(first_name, last_name)
    @balance = 0
    @first_name = first_name
    @last_name = last_name
  end

  def full_name
    "#{@first_name} #{@last_name}"
  end
end

In self.create_for you are creating a new account and putting it into the variable @accounts which is "global" (a class variable) inside BankAccounts. Your new instance of a bank account is stored in @accounts so when you use self.find_for, it can look in @accounts to find it there.

@accounts.find{|account| account.full_name == "#{first_name} #{last_name}"} essentially means:

"Look into the @accounts Array, and for every account instance (that is, an instance of BankAccount) see if the full name matches. full_name is a method I may use to inspect a BankAccount instance."

Sign up to request clarification or add additional context in comments.

Comments

1

Because you code it like that.

Look the line @accounts << BankAccount.new(first_name, last_name).

It means @accounts holds all BankAccount objects. Now full_name is an instance method of the instances of the class BankAccount.

Now in the below line -

 @accounts.find { |account| account.full_name == "#{first_name} #{last_name}"}

#find method passing each such BankAccount object(account) to the block, and account is calling the method full_name, being an instance of the class BankAccount.

Nothing wrong here.

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.