Gem Content3:30 with Jason Seifer
Occasionally, you may want to know how a gem functions internally. In this video, we'll see how to look at the contents of a gem.
Sometimes, as you're coding and using a gem, you may want to get a better idea 0:00 of how it was written or take a look at a function or method. 0:05 RubyGems gives us this ability with a couple of different commands. 0:09 We can see the location of gems and open them in our editor to see how they work. 0:13 Let's go ahead and see how that works now using Workspaces. 0:18 So if we want to see the contents of a gem, we can use the contents command. 0:22 And we pass in the name of the gem. 0:27 So by typing, gem contents money, 0:31 this will list everything that is inside of the money gem. 0:34 These are all of the different files. 0:38 Authors, change log, contributing, gem file, etc. 0:40 We can also see all the library files that make up the money gem. 0:45 The gemspec, which tells Ruby about the different versions and 0:50 dependencies, and the different tests. 0:55 Let's go ahead and change to that directory. 1:00 So we can do that by saying, 1:04 cd /home/treehouse/.local/gems/gems/mney-6.- 1:06 6.1. 1:15 And now we are inside of the money gem. 1:16 If we wanted to get a list of how this works, we could use the ls command, 1:23 and we can see that the only file listed is money.rb. 1:29 Which requires everything from this directory inside of the money directory. 1:34 So if we go a little bit further and 1:41 we list the files, we should see that these should be included as well. 1:43 So we require money/money and money/currency. 1:48 So if I look at the money.rb file, 1:56 here is the different Ruby code that makes up the Ruby money gem. 2:00 Now, if we do some syntax highlighting here with vim, 2:12 we can walk through the money class, and 2:17 see how it's coded if we wanted to get an idea of how money works. 2:20 So we can see that the sent method returns a fractional or 2:25 an integer with infinite precision false or 2:30 big decimal with infinite precision true. 2:36 Now, we're only going to need to look at this documentation or 2:40 these methods if we need to. 2:44 So if we're writing a program and we come across something that we don't understand, 2:47 it's very useful to be able to look at the source code of the gem that we're using. 2:51 Then we can also see this requires different files inside of the gem. 2:57 So if I wanted to look at how the formatting worked, 3:09 we could look at the formatting.rb file. 3:12 And we can see how this class is set up. 3:14 For the most part, we don't need to do this when we're working with gems, but 3:18 it is useful to know where gems are installed, located, and 3:23 how they're written. 3:27
You need to sign up for Treehouse in order to download course files.Sign up