1 00:00:00,000 --> 00:00:07,000 [??] [Treehouse] 2 00:00:08,000 --> 00:00:11,000 In this badge, we're going to be talking about Ruby Core. 3 00:00:11,000 --> 00:00:17,000 Ruby Core is a collection of classes and modules that come with your Ruby installation. 4 00:00:17,000 --> 00:00:19,000 So let's get started. 5 00:00:20,000 --> 00:00:23,000 Now we're going to take a look at symbols in Ruby. 6 00:00:23,000 --> 00:00:26,000 Symbols in Ruby are very similar to strings. 7 00:00:26,000 --> 00:00:31,000 However, one of the big differences is that symbols only exist in memory 8 00:00:31,000 --> 00:00:34,000 once during a session in Ruby. 9 00:00:34,000 --> 00:00:36,000 Let's take a look at how that works. 10 00:00:36,000 --> 00:00:39,000 Go ahead and launch IRB right now. 11 00:00:39,000 --> 00:00:43,000 I'm going to create a hash called treehouse. 12 00:00:43,000 --> 00:00:46,000 At first, I'm going to use strings for hash keys. 13 00:00:58,000 --> 00:01:00,000 Now we have our treehouse hash. 14 00:01:00,000 --> 00:01:05,000 If we wanted to redo this hash using symbols instead of using quotes, 15 00:01:05,000 --> 00:01:07,000 we would use a colon. 16 00:01:07,000 --> 00:01:09,000 Let's see what that looks like. 17 00:01:20,000 --> 00:01:24,000 Now we have basically the same thing as our first hash. 18 00:01:24,000 --> 00:01:28,000 However, our hash keys are symbols. 19 00:01:28,000 --> 00:01:34,000 The difference is if we made another hash--let's say called treehouse2-- 20 00:01:34,000 --> 00:01:39,000 the Ruby interpreter would not create new objects each time. 21 00:01:39,000 --> 00:01:41,000 In the first version where we use strings, 22 00:01:41,000 --> 00:01:47,000 each time we initialize a hash key, a new string object is created. 23 00:01:47,000 --> 00:01:52,000 However, when we use symbols, we're always referring to the same symbol. 24 00:01:52,000 --> 00:01:56,000 And there are a couple of different ways of creating hash keys as symbols as well. 25 00:01:56,000 --> 00:02:01,000 In newer versions of Ruby, from 1.9.2 and up, 26 00:02:01,000 --> 00:02:04,000 you don't have to put a colon in front of the symbol 27 00:02:04,000 --> 00:02:07,000 in order to denote it as a hash key. 28 00:02:17,000 --> 00:02:22,000 And we can see that that produces the same symbols as before. 29 00:02:29,000 --> 00:02:33,000 Now let's see how we might use this in one of our Ruby programs. 30 00:02:38,000 --> 00:02:43,000 I'm going to put all of these examples in a directory called core. 31 00:02:50,000 --> 00:02:54,000 Now let's create a very simple class called BankAccount 32 00:02:54,000 --> 00:02:57,000 and show how to use symbols. 33 00:03:14,000 --> 00:03:19,000 In our deposit method, we're going to use an options hash. 34 00:03:19,000 --> 00:03:21,000 The options hash is going to have a default value 35 00:03:21,000 --> 00:03:25,000 that's going to let us add a memo to our deposit. 36 00:03:27,000 --> 00:03:35,000 So what we'll do is conditionally assign the memo to the phrase "Regular deposit." 37 00:03:37,000 --> 00:03:41,000 Then we're going to add on to these transactions. 38 00:03:54,000 --> 00:03:57,000 Now we can go ahead and instantiate our bank account. 39 00:04:06,000 --> 00:04:10,000 Now when we call the deposit method, we can pass in an amount. 40 00:04:10,000 --> 00:04:15,000 If we don't pass in a memo, it will just add the default memo. 41 00:04:15,000 --> 00:04:18,000 Let's go ahead and call deposit again, 42 00:04:18,000 --> 00:04:21,000 but this time we'll send in a memo as well. 43 00:04:34,000 --> 00:04:38,000 And just for fun, let's see what our bank account looks like. 44 00:04:41,000 --> 00:04:44,000 Let's go ahead and run this program and see what happens. 45 00:04:47,000 --> 00:04:50,000 And we can see that in our transactions array 46 00:04:50,000 --> 00:04:54,000 at first we got the memo of "Regular deposit," 47 00:04:54,000 --> 00:04:58,000 and in the second transaction we had the memo "This was a gift." 48 00:04:58,000 --> 00:05:04,000 Each time, the amount and memo symbols are not recreated. 49 00:05:04,000 --> 00:05:07,000 They're referencing the same symbol in memory. 50 00:05:07,000 --> 00:05:10,000 And finally, one reason that you might use symbols 51 00:05:10,000 --> 00:05:13,000 is because if you're going to define a method name, 52 00:05:13,000 --> 00:05:15,000 you can use a symbol. 53 00:05:15,000 --> 00:05:19,000 By defining a method, the symbol will exist in memory anyway. 54 00:05:19,000 --> 00:05:22,000 And that's how you work with symbols in Ruby. 55 00:05:22,000 --> 00:05:26,000 In our next video, we're going to be working with dates and times. 56 00:05:26,000 --> 00:05:29,000 [??] [Treehouse]