Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Mark Josephsen
Mark Josephsen
8,803 Points

'self.find_for' problem with Ruby Foundations: Class Methods

I can't get past 4:34 of this video. I don't understand what is going wrong. I'm running this in a terminal on Ubuntu 14.04 32-bit. I wish I could copy and paste my terminal, but this forum won't display it correctly. Assume I've followed the video exactly to 4:34. When I use the find.for method, it returns nil.

I have no idea why it's nil. Here is my bank_account.rb code:

class BankAccount

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

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

def deposit(amount)
    @balance += amount
end

def withdraw(amount)
    @balance -= amount
end

def balance
    @balance
end

end

Any help is appreciated. Thanks!

Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

I'm adding the link of video here.. Ruby Foundations: Class Methods And on Ubuntu you may be able to copy and paste with right click? How was it like when you did create_for? I wish I could see the console...

4 Answers

Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

Thank you Mark. The method self.find_for in your code had first_name and last_name as local variables, not instance variables. Sorry I didn't see that at first.

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

not

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

Aha! Got it. Thank you so much!

Mark Josephsen
Mark Josephsen
8,803 Points

I'm trying my best to paste it. The problem is not that I can't copy it, it's this forum. Every time I post my copied terminal code, it appears differently. This forum modifies it. I'll try some different ways to see if I can get it on here correctly.

Mark Josephsen
Mark Josephsen
8,803 Points

vimmington@vimmington-G41D3:~$ cd Desktop

vimmington@vimmington-G41D3:~/Desktop$ cd Source

vimmington@vimmington-G41D3:~/Desktop/Source$ cd methods

vimmington@vimmington-G41D3:~/Desktop/Source/methods$ irb

irb(main):001:0> load './bank_account.rb'

=> true

irb(main):002:0> BankAccount.create_for("Bob", "Dylan")

=> [ # < BankAccount:0x8583ccc @balance=0, @first_name="Bob", @last_name="Dylan" > ]

irb(main):003:0> BankAccount.create_for("Some", "Guy")

=> [ # < BankAccount:0x8583ccc @balance=0, @first_name="Bob", @last_name="Dylan" >, # < BankAccount:0x858b01c @balance=0, @first_name="Some", @last_name="Guy" > ]

irb(main):004:0> bank_account = BankAccount.find_for("Some", "Guy")

=> nil

irb (main):005:0 > bank_account.deposit(100)

NoMethodError : undefined method `deposit' for nil : NilClass

f rom (irb) : 5

f rom /usr/bin/irb:12:in `<main>'

irb(main):006:0>

Mark Josephsen
Mark Josephsen
8,803 Points

Ok so there, I've added a bunch of random spaces so that this forum won't post the output differently. I'm not sure why the 2nd and 3rd to last lines are different, but aside from the added spaces, they're accurate. So, you'll see that the problem starts when bank_account = BankAccount.find_for("Some", "Guy") returns nil. Thank you for your help!