# Make your own gem.

Ruby is a high-level, dynamic programming language. Ruby-1.9 or more comes with built-in [**Rubygems**](https://github.com/rubygems/rubygems) software. The Rubgems software is a package/library manager that manages other ruby libraries for your project, these packages are known as "**gems**" in the ruby world. Gems are used to enhance the capabilities of the existing ruby application. Rubygems software is packed into a command called "**gem**".

### Let's make your first gem called "learn\_to\_code"

Create a file with the name `learn_to_code.rb` in a lib folder and another file with the name `learn_to_code.gemspec` in a working directory.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683863077215/c4550497-78ca-4d5b-8c20-9bc784669ade.png align="center")

The code for the gem will be inside the lib folder, and the convention is that one file has a name similar to the gem so that it will get loaded when we call `require "learn_to_code"`.

```ruby
class LearnToCode
  def self.start
    puts "Happy Learning!!"
  end
end
```

Metadata for the gem goes into a gemspec file.

```ruby
Gem::Specification.new do |s|
  s.name        = "learn_to_code"
  s.version     = "0.0.0"
  s.summary     = "Learn computer programing by using ruby language"
  s.description = "Learn computer programing!"
  s.authors     = ["Mukesh"]
  s.email       = "ashu.3333d@gmail.com"
  s.files       = ["lib/learn_to_code.rb"]
  s.homepage    = "https://rubygems.org/gems/learn_to_code"
  s.license       = "MIT"
end
```

Now to create a package run the command `gem build learn_to_code.gemspec`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683865470801/b3ef61cb-4d11-41cc-937a-39eaec58aad0.png align="center")

To install the package locally run the command `gem install ./learn_to_code-0.0.0.gem`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683865565541/331303df-8862-412c-adb8-527023285c6b.png align="center")

To test the gem, open `irb` and require the gem that we installed earlier.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683865757918/cb7696c7-5d9c-4e4c-a2b9-1929e85a55ad.png align="center")

Now to publish your gem you need to signup for an account at Rubegems.com.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683866320273/fac9d4cf-3d13-49f9-b8bb-6845b912ba9b.png align="center")

Push your gem to Rubygem's repository by using the command `gem push learn_to_code-0.0.0.gem` and it will be available for everyone to use.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683866337583/4a85d33c-34da-4af0-bb2d-7d2556f5c9cd.png align="center")

### **How to add more files**

Add two classes `ComputerArchtiecture` and `DesignPattern` under into `courses` folder, and modify `LearnToCode` class that requires those classes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683949429215/3473c9ea-adbf-4991-8a25-719e9d716140.png align="center")

```ruby
class DesignPattern
  def self.start
    puts "Happy Design Pattern Learning!!"
  end
end
```

```ruby
class ComputerArchtiecture
  def self.start
    puts "Happy Computer Archtiecture Learning!!"
  end
end
```

```ruby
require "courses/computer_architecture"
require 'courses/design_pattern'

class LearnToCode
  def self.start(cource="Happy Learning!!")
    case cource
    when "Computer Archtiecture"
      ComputerArchtiecture.start
    when "Design Pattern"
      DesignPattern.start
    else
      puts "#{cource}"
    end
  end
end
```

Do not forget to update `s.files` with the paths of newly added classes.

```ruby

Gem::Specification.new do |s|
  s.name        = "learn_to_code"
  s.version     = "0.0.1"
  s.summary     = "Learn computer programing by using Ruby language"
  s.description = "Learn computer programing!"
  s.authors     = ["Mukesh"]
  s.email       = "ashu.3333d@gmail.com"
  s.files       = ["lib/learn_to_code.rb", "lib/courses/computer_architecture.rb", "lib/courses/design_pattern.rb"]
  s.homepage    = "https://rubygems.org/gems/learn_to_code"
  s.license       = "MIT"
end
```

### The naming convention for the gem

| Gem name | require statement | class name |
| --- | --- | --- |
| Use underscores for multiple words e.g. `learn_to_code` | `require 'learn_to_code'` | `LearnToCode` |
| Use dashes to name a gem if it is inside the directory e.g. `learn-to-code` | `require 'learn/to/code'` | `Learn::To::Code` |

Don't use uppercase letters to name a gem because Windows and OSX are case-independent but it will not work in other operating systems.
