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

Instant and Class Methods?

Hi helpers out there.....I've been following Jason Seifer's Ruby Basics step by step and it has been very helpful....but I got to the instant/class methods videos and I felt like there were a number of functions that weren't fully explained............ here's a portion of the code for the BankAccount class below, and after that I will point out what wasn't very clear in the videos (to me at least)

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 full_name "#{@first_name} #{@last_name}" end

so, I understand that he wanted to create an empty array so that bank accounts can be stored with the following code @accounts | | = [ ]

but what do the empty absolute values represent? why are they there?

@accounts << BankAccount.new(first_name, last_name)

I'm guessing this is the part where the entire array gets registered into the class, however what does << mean? how would i know when to use << as opposed to =

also, the code that's used to be the engine for finding a specific account and loading it up to be used for transactions, was just not explained at all. can anyone go through the account.full_name part..and how the value account gets linked to the full_name method?

THANKS!

1 Answer

The self.create_for method allows you to call the method on the class itself, rather than an object of the class. That means you can call the method before an object of that class has been made. Before, remember that you had to make an object of that class with BankAccount.new before you could call methods on it.

The Account Managers accounts ||= [ ] says create an empty array if @accounts isn't already created.

The code << is just telling ruby to append to the @accounts array with the new bank account. I'd go into your console and make an array and play around with it, it would help you a lot.

a = [ 1, 2, 3 ]
a << 4
a #would output the array with the new 4 value appended to the end so [1, 2, 3, 4]

The self.find_for method allows you to type in a first and last name and it will return the correct BankAccount object if it exists. The way that works is by calling the find method on the @accounts array which accepts a block and we individually go into each element of that array(which is assigned as a local variable called 'account' and see if it matches the name that we gave as an argument to the self.find_for method, if it finds a match, it returns that object. the self.find_for method has access to the full_name method because it was defined in the same class, all instance methods can be used within each other.

I'm not the best at explaining but go ahead and let me know if this doesn't clear it up well enough for you. Don't worry, this stuff all snaps into place :)

Thank you for taking the time to help me out! I definitely understood everything you said, but when I tried to put my own twist I got into some trouble. Here's what I'm trying to do with the BankAccount example:

I want Ruby to ask the user if 'he/she' has an account already, in which the user either says or no. If he says no, then it will prompt him to put in his name....and that name gets registered where he can deposit/withdraw money. And if the user already has an account, he would say 'yes' and it will prompt him/her to put in their name so that it pull up their account (and here's where I use the .find method.

Mainly, i'm getting errors with the line that has << and the .find line (which are the key elements)

This is my code : Thanks again!!

'''

class BankAccount def self.create_for @accounts ||=[] end

def initialize @balance=0

end

def h puts "do you have an account with us?" ansa=gets.chomp if ansa=="no" puts "ok welcome, put your name" username=gets.chomp @accounts << BankAccount.new(#{username})

puts "welcome #{username} let's deposit some money \n"

deposit

end

if ansa=="yes"

find_for

end

def self.find_for puts "okay put your previously given name"

givenname=gets.chomp

@accounts.find{|account| account.givenname== "#{@accounts}" end

end

def deposit puts "enter the amount in USD" aaron=gets.chomp aaron=aaron.to_i @balance+=aaron

puts "your balance is #{@balance}"

h

end

'''