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!
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
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?
Sam Donald36,305 Points
class MyClassName def initialize(string1, string2) @string1 = string1 @string2 = string2 end end
Who the hell knows what MyClassName is about???
class UserName def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end end
Now that's something you can wrap your head around.
Susan Mashevich3,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.