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 Ruby Objects and Classes Build a Bank Account Class Part 1: Create the Class

Why do we type initialize(name) when it could be initialize(string) and still does the same thing in this program....

Whereas in the last program we had to include (title, first_name, middle_name, last_name) in the initialize for the program to work. I just don't understand what's going on here. Why can you put in anything in the parenthesis?

2 Answers

Sam Donald
Sam Donald
36,305 Points


class MyClassName
    def initialize(string1, string2)
        @string1 = string1
        @string2 = string2

Who the hell knows what MyClassName is about???

Where as,

class UserName
    def initialize(first_name, last_name)
        @first_name = first_name
        @last_name = last_name

Now that's something you can wrap your head around.


Susan Mashevich
Susan Mashevich
3,623 Points

The initialize method is very important in that it is called immediately when you create a new object so that object can inherit attributes set in the initializer method. Attributes (kind of like characteristics/traits) will be inherited by objects created from that class and are meant to describe the objects created.

So, for example if you have a class called "Human", yes you can just put the parameter 'string' and it will 'work', but when you try to instantiate an object from this class (eg; bob = Human.new('this is a string') ) that will be ONLY piece of information you have to describe that object in your program (when you create an account on a website, usually they ask for a bunch of more info like, first name, last name, email, etc -> these are attributes of a user, which get defined when a new user is created).

This is why usually the initialize method should have multiple parameters depending on the program, and since there are multiple parameters you will want to use on multiple objects to avoid complete confusion you'd probably want them to be more descriptive. Sorry if this was too wordy, hope it helped.