Definition of RubyGems
RubyGems is a library manager in the Ruby applications, it allows us to manage all the library without holding the actual library inside the application, which means we do not require to keep the library files inside the applications whenever we required we can use a command of RubyGems called gem install, gem install will install all the relevant library for us which is written over gemfile, here gemfile is the file which maintains the names of library and when we run the command gem install it will install the library, because of this application will be portable and small in size because we do not require to keep all the library inside the application as we can download all them with gem install command.
In the below syntax we can see we have written simple syntax we can explain the syntax in a few steps.
gem command-name optional-value
- gem: This is the main command for GemFile, this gem is common for all the commands.
- command-name: Here command-name is the name of operation which we want to perform, so it may be we want to install, uninstall, or to check the environment of Ruby application.
- optional-value: This is an optional value, in this section we can pass some required and optional attributes.
Commands of RubyGems
Following are the commands are given below:
1. gem environment
Suppose we wanted to see all the details related to the environment then we can use this command. Here the environment can be anything like RUBYGEM version, path, configurations, source, etc. .With the help of this command during development, we can understand the basic structure without even opening the whole application. Please see the below example along with the screen of output.
2. gem dependency
It shows us all the library inside the gemfile. Many times during development we may need to know the file available inside the gemfile. It will not show only the name of the dependency it also shows us the versions of all the dependency. The main uses of this command it allows us to understand the current version of the dependency library which we are using and if there is any fault with the given version. Because many times we may be using different versions of the gem file and library version which is there may not be supported by gem show by knowing the correct versions of the library we can analyze this. We can even do it for a single dependency instead of displaying all the dependency. Please see the below example along with the screen of output.
3. gem install
This is the most important command and highly used command in the gem library. It allows us to install all the library dependencies on the gemfile. This command will go through the file called Gemfile and find all the library dependencies with the exact version of it and install that library on the application. The main benefit of this command is it allows us to install all the library and hence we do not require to keep the library files inside the application when we are moving an application from one system or location to another, it also makes an application in smaller in size without we are not keeping library files inside the application we are only maintaining the names of the files inside the gemfile. Please see the below example along with the screen of output.
gem install redis
4. gem fetch
This command will be used to download the gem and it will remove the older one with the downloaded one.Please see the below example along with the screen of output.
gem fetch redis
5. gem help
It gives us all the activity and useful things related to the gem command. Please see the below example along with the screen of output.
6. gem owner GEM
To run this command we need to create an account to the gem server and we must get login then only we can be able to run this command. In the below example you can see tried to run the command and it showing Access denied because I do not have an account on the gem server. Here the gem server is the place where all the library is available. You can also maintain your own library and you push and do some changes over there. Please see the below example along with the screen of output.
gem owner GEM
7. gem sources
Manage the sources and cache file RubyGems uses to search for gems. It shows the place where it manages all the gem file. You just think of this if we are not maintaining the library inside our application, which means we are only keeping the names of the file inside the gemfile and whenever we needed we are running the command gem install and that will install the dependency. But here the question is from where these all library will come what will be the source of the library? So to get the answer we can run the command gem source. The common gem source will show the library from where our application will fetch when we run the command gem install. We can see in the below example it is showing us the source as the https://rubygems.org/ as the source. Please see the below example along with the screen of output.
8. gem which FILE
Suppose you have downloaded all the required libraries and now we want to see the path of the downloaded library then we can use this command. See in the below example we want to see the path of the installed redis library. It is displaying us the exact path of the library which we have searched for. Please see the below example along with the screen of output.
gem which redis
From the tutorials, we learned about the RubyGem and we learned about the commands used in the Ruby gems. We mostly focus on the uses of the various commands of Rubygems and also learned about how Rubygems manages all the libraries of our applications.
This is a guide to RubyGems. Here we also discuss the definition and syntax along with commands of RubyGems which includes gem environment, gem dependency, etc. You may also have a look at the following articles to learn more –