1 00:00:00,000 --> 00:00:04,661 [MUSIC] 2 00:00:04,661 --> 00:00:08,330 So far, we've learned about blocks in Ruby and how powerful they are. 3 00:00:09,380 --> 00:00:12,150 Now we're going to put all of that knowledge to the test and 4 00:00:12,150 --> 00:00:14,880 write a program using work spaces. 5 00:00:14,880 --> 00:00:18,530 We'll be working with Ruby's built in classes, writing our own classes and 6 00:00:18,530 --> 00:00:21,650 methods, and practicing with blocks. 7 00:00:21,650 --> 00:00:22,780 So let's go ahead and get started. 8 00:00:23,990 --> 00:00:28,530 Okay, so let's go ahead and create our own class that uses blocks and 9 00:00:28,530 --> 00:00:30,920 hashes and strings and everything we've learned so far. 10 00:00:32,020 --> 00:00:36,470 What we're going to do is make a class for a monster. 11 00:00:36,470 --> 00:00:40,160 And a monster is going to be able to do a few different things. 12 00:00:40,160 --> 00:00:43,230 And we will do these things using blocks. 13 00:00:43,230 --> 00:00:46,495 So, let's go ahead and create monster.rb. 14 00:00:47,550 --> 00:00:53,460 So, first step is to define the class. 15 00:00:53,460 --> 00:00:55,210 And the class is Monster. 16 00:00:57,450 --> 00:00:58,890 We're going to go ahead and 17 00:00:58,890 --> 00:01:04,650 give the monster a name when we initialize the class. 18 00:01:05,720 --> 00:01:08,932 And, let's go ahead and just set that to an instance variable. 19 00:01:10,932 --> 00:01:13,644 And make an attr_reader out of that so 20 00:01:13,644 --> 00:01:17,472 we don't have to type the at sign inside of the class. 21 00:01:20,932 --> 00:01:23,820 So, first thing that we'll do? 22 00:01:23,820 --> 00:01:27,207 Let's go ahead and make the monster say something. 23 00:01:32,547 --> 00:01:37,700 And the monster will say something, and then execute whatever is in the block. 24 00:01:37,700 --> 00:01:42,180 We're going to assume that when somebody calls this, 25 00:01:42,180 --> 00:01:45,800 they are going to be printing something to the screen. 26 00:01:45,800 --> 00:01:50,450 So we'll just go ahead and print the name says, so 27 00:01:51,540 --> 00:01:56,440 that would be the name of the monster, and the word says. 28 00:01:56,440 --> 00:01:59,672 So let's go ahead and create a new monster here. 29 00:01:59,672 --> 00:02:03,800 And we'll give this monster a very intimidating monster-like name. 30 00:02:06,020 --> 00:02:06,520 Fluffy. 31 00:02:07,660 --> 00:02:13,893 And, the monster will say. 32 00:02:13,893 --> 00:02:15,120 What's the monster gonna say? 33 00:02:15,120 --> 00:02:17,300 What's something scary that a monster would say? 34 00:02:19,280 --> 00:02:20,870 Welcome to my home. 35 00:02:24,040 --> 00:02:25,010 Let's go ahead and save that. 36 00:02:26,770 --> 00:02:27,270 And run it. 37 00:02:30,490 --> 00:02:31,820 Oh, nothing happened. 38 00:02:31,820 --> 00:02:33,010 Why? 39 00:02:33,010 --> 00:02:36,370 Because we didn't call the block or yield to it. 40 00:02:37,970 --> 00:02:41,220 So let's go ahead and yield, and run this again. 41 00:02:44,080 --> 00:02:45,570 So there we go, Fluffy says... 42 00:02:45,570 --> 00:02:47,870 Welcome to my home. 43 00:02:47,870 --> 00:02:49,710 And that's a pretty good start. 44 00:02:49,710 --> 00:02:55,810 So let's go ahead and start implementing some more actions in the next video.