1 00:00:00,590 --> 00:00:04,120 Now let's go ahead and write our first module. 2 00:00:04,120 --> 00:00:06,820 Writing a module is very similar to writing a class. 3 00:00:07,940 --> 00:00:12,180 Modules follow the same rules as classes and use camel case for naming. 4 00:00:13,455 --> 00:00:17,835 When we create a module, we use the Ruby keyword Module, 5 00:00:17,835 --> 00:00:21,995 followed by the name of the module in CamelCase and then the keyword end. 6 00:00:23,095 --> 00:00:25,555 Let's see how that works now using WorkSpaces. 7 00:00:27,375 --> 00:00:31,469 Okay, so let's go ahead and see how to write a Ruby Module. 8 00:00:32,750 --> 00:00:37,170 Now I've gone ahead and launched a new Ruby workspace. 9 00:00:37,170 --> 00:00:40,790 So go ahead and close this welcome markdown file. 10 00:00:42,190 --> 00:00:46,710 Now over on the left side right click and choose new file. 11 00:00:48,180 --> 00:00:53,970 We're going to name this simple_module.rb And 12 00:00:53,970 --> 00:00:56,580 now, it will open up in the main text editor. 13 00:00:58,050 --> 00:01:02,920 Writing a module is just like writing a class except instead of 14 00:01:02,920 --> 00:01:08,070 using the class keyword, we use module, so 15 00:01:08,070 --> 00:01:12,960 type in the module keyword and then the name of the module that you want. 16 00:01:14,080 --> 00:01:18,390 Now I'm going to call this SimpleModule and 17 00:01:18,390 --> 00:01:20,850 then press enter and write the word end. 18 00:01:22,670 --> 00:01:25,370 Now let's go ahead and see how this works inside of irb. 19 00:01:26,390 --> 00:01:30,540 Click down into the console on the bottom of the screen and type in irb. 20 00:01:32,020 --> 00:01:37,240 Now we have to get our simple module program loaded into irb. 21 00:01:37,240 --> 00:01:42,330 We do that by typing load, then a space, quote and 22 00:01:42,330 --> 00:01:45,860 then the path to the simple module file. 23 00:01:45,860 --> 00:01:49,430 Which is right here so type ./ and then the name of the file, 24 00:01:49,430 --> 00:01:55,210 which is simple_module.rb and irb will return true. 25 00:01:56,700 --> 00:01:59,940 So now you might wonder how we access simple module. 26 00:01:59,940 --> 00:02:02,980 Now we can't initialize it like a class. 27 00:02:02,980 --> 00:02:05,900 So normally we could say class.new, but 28 00:02:05,900 --> 00:02:09,770 we can't do that when we're working with modules. 29 00:02:09,770 --> 00:02:13,065 Just accessing it will return SimpleModule. 30 00:02:14,400 --> 00:02:16,510 So this doesn't do much right now, but 31 00:02:16,510 --> 00:02:19,920 what we could do if we wanted to was store a constant in here. 32 00:02:21,130 --> 00:02:26,455 So lets say we wanted to return the version of this SimpleModule. 33 00:02:27,630 --> 00:02:34,809 We could create a constant called VERSION and set that equal to 1.0. 34 00:02:35,980 --> 00:02:40,100 Now constants are their own thing, in Ruby kind of like modules, 35 00:02:40,100 --> 00:02:43,800 they're written in all upper case, and they can't be changed. 36 00:02:44,850 --> 00:02:47,925 So once we type in this version here, it's gonna be locked at 1.0. 37 00:02:47,925 --> 00:02:51,485 Now since we've changed this file and 38 00:02:51,485 --> 00:02:55,805 saved it, we need to reload it into our irb session. 39 00:02:55,805 --> 00:02:58,235 So I'm going to clear the screen and in order to do that, 40 00:02:58,235 --> 00:03:00,705 I'm going to press the up arrow a couple of times 41 00:03:00,705 --> 00:03:03,765 until that load statement comes back up and there we go. 42 00:03:04,935 --> 00:03:08,735 Now if we want to access the version inside of this module, 43 00:03:10,110 --> 00:03:13,410 we do that by typing SimpleModule, and 44 00:03:13,410 --> 00:03:19,040 then two colons, and the word VERSION and that will return 1.0. 45 00:03:19,040 --> 00:03:25,320 When we access something using with two colons, it's called the constant 46 00:03:25,320 --> 00:03:30,509 resolution operator, and that's what we use to access something inside of modules. 47 00:03:31,600 --> 00:03:35,245 Try creating a module on your own now using WorkSpaces.