![]() |
A Gemfile is a configuration file that specifies the gem requirements needed to run a Ruby program. A Gemfile should always be located at the root of the project directory. To create a Gemfile we usually use a bundler which is a popular gem management tool. This article focuses on discussing steps to create a gemfile in Ruby. Steps to Create a Gemfile in RubyTo create a Gemfile for our Ruby project, follow these simple steps: Step 1: Create a New Directory (Optional)We will start by creating a new directory for our project using the `mkdir` command in our terminal:
We use the “mkdir”(make directory) command to create a new directory in a command line environment. Then “cd” command to “change directory” to the folder we created earlier on. ![]() Creating a new folder and changing the directory. Step 2: Initialize a New Ruby ProjectLet’s now initialize our project using the following command if not initialized before:
This command will create a new file named Gemfile in our project directory.
The above output shows that our ‘Gemfile file’ has been created. Step 3: Edit the GemfileWe will now open the Gemfile using a text editor of our choice e.g VScode. And add gem dependencies to this file using the `gem` key-word. For instance, let’s add the ‘faker’ and `colorize` gems as dependencies in our Gemfile:
Our gemfile will look as shown above. The “faker” gem is used to generate fake data for testing or seeding a database, while “colorize” is useful for adding color to text output in the terminal. Step 4: Specify Gem Versions (Optional)We can specify the version of the gem we want to use by adding it after the gem name. For example:
This ensures that when we run bundle install, it will install version 3.3.1 specifically, and our project will use that version. This is a common practice to avoid potential issues with breaking changes in newer major versions. ![]() Faker version 3.3.1 Step 5: Install GemsOnce we’ve added all the gems we need to our Gemfile, let’s save the file and run the following command in our terminal to install them:
This command downloads and installs all the gem given in our Gemfile, as well as its dependencies, and creates a `Gemfile.lock` file to lock the version of the gem. ![]() Installing dependencies. ![]() Gemfile.lock file Step 6: Using the Gems in Our Ruby CodeLet’s use the installed gems by requiring them:
Example:Lets create an `app.rb` file and add the following code:
The code above utilizes the Faker gem to generate fake names and the Colorize gem to add color to the output in the terminal. When we run this Ruby script, it will print a welcome message in green and generate 10 fake names, each in blue color. This will add some visual appeal to our output. To run our demo app we will use the following command:
Running ‘ruby app.rb’ executes the Ruby script `app.rb`. Output: ![]() Output That’s it! We’ve created a Gemfile for our Ruby project and installed our gem dependencies using bundler. |
Reffered: https://www.geeksforgeeks.org
Ruby |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |