addis_a
September 17, 2014, 1:11pm
1
Buck class
Def talk
Puts “Say hello”
End
End
Bo = buck.new
Bo.talk
Results: say hello
Now, I understand what I’ve written here but what I don’t
Understand is:
Bo = buck.new
What am I doing here? Bo is a object I created, I get that but why do I
need to write = buck.new
That code wouldn’t run anyway, it’s written incorrectly.
The reason you need to “instantiate” an instance of the class is usually
explained by demonstrating with people.
class Person
attr_accessor :name
def initialize( input_name )
self.name = input_name
end
end
If you wanted individual people, each would be a unique Person, so:
elysee = Person.new “elysee”
joel = Person.new “joel”
[elysee, joel].each { |person| puts person.name }
Every “new” command creates a different Person, but they’re all
instances of the Person class, even though they have different names.