- Read Tutorial
- Watch Guide Video
In this lesson, we are going to talk about Ruby Gems and how we can use it in our applications.
Before going into gems, I want to briefly talk about two common approaches that developers have towards them. Firstly, some developers think they don't need to write code at all, and all that they have to do is find a Ruby Gem for every feature in the application. That is not the right way to code because you'll be spending more time finding a gem than building the application. The second approach is that some developers look down on gems, and don't want to use them at all in their code. That's not a good idea either because some talented people have already spent time to create gems so that you don't have to spend time and effort doing the same.
So, the best way to go about it is to find a good balance between these two approaches. For example, I like to use a gem called Devise
for my authentication features. It's a convenient gem that allows me to do 90% of the things I want, but then I can also customize it within my code when needed.
Now, if you go to your Gemfile
located at the root of your system, you can see there are many gems here.
If you notice, rails
is also a Ruby gem because at the heart, Rails is a set of code libraries and modules that are called to do many different things in the application. Each of these gems contain Ruby code that give you a certain functionality through the many methods provided in it. Technically, you can copy the code present inside a gem into your application and access them like you built those methods from scratch, though I won't recommend doing that. However it's important to think about gems in that way since it takes the magic out of them. Gems are magic, they're simply Ruby code libraries, exactly like the code you write inside your application.
We'll see more about gems in the upcoming videos.