Join Kevin Skoglund for an in-depth discussion in this video Initialize method, part of Ruby Essential Training.
Very often in Ruby when we create an instance of a class, we want that instance…to do some initialization, settings some default values or calling some default…methods that will happen right at the beginning.…We saw a little bit of this in our last movie, when we wrote the set_limbs…method, and that was a method that we called that set the values of the legs and…arms, instance variables on our animals so that they had a default value there.…Well, instead of calling a separate method that will set those up, let's instead…use the initialize method.…So here I'm in the Animal class that I've been working with.…
Creating an initialize method is just as easy as calling initialize.…That's all there is to it.…Now I no longer have to call setup_limbs, boom! Done!…It's just like that. That simple.…Now initialize will set any values that I want here.…It will also allow me to execute some other Ruby code, I can call other things in here.…Maybe I want to output something.…Let's try puts "A new animal has been instantiated".…
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish
Skill Level Beginner
1. Getting Started with Ruby
2. Ruby Object Types
3. Control Structures
4. Code Blocks
8. Working with Files
9. Ruby Project: Creating the Food Finder
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.