How To Create Gem In Rails

Total Blog Views: 116

Blog Status: publish

Created By: swaz_ahmed Created at: 06-23-2024

Tags: ror gem

  • What is Gem 
  1. Gem is the open source library that contain Ruby code and are packaged with a little extra data . 
  2. Gem allows a programmer to use the code within the gem in their own program, without explicitly inserting that code.

There are several gems as follows:  

  1. RSpec — A testing framework that supports Behavior Driven  Development for Ruby.
  2. Devise — Devise works with authentication. For any website that needs to user log-in’s, Devise handles sign in, sign up, reset password, etc

Generate the Gem Skeleton: Use the Bundler gem to generate a new gem skeleton.

Run: bundle gem hello_world_gem

 

bundle gem  hello_world_gem 

File Structure:

  • lib:Directory where your gem's code resides. hello_world_gem.rb: Ruby file containing your gem's code.
  • spec (optional): Directory for your gem's test suite.
  • Gemfile and Gemfile.lock: Files that Bundler uses to manage your gem's dependencies.
  • hello_world_gem.gemspec: Gem specification file where you define metadata about your gem.
  • README.md: File containing documentation for your gem.

Implement the Code :

  • Implement the "Hello, World!" functionality. Open the lib/hello_world_gem.rb file

 

module HelloWorldGem
        def self.say_hello
          puts “Hello,World!”
      end
end

Setup the .gemspec configration:

 

require_relative "lib/hello_world_gem/version"

Gem::Specification.new do |spec|

spec.name = "hello_world_gem"
spec.version = HelloWorldGem::VERSION
spec.authors = ["test test"]
spec.email = ["[email protected]"]
spec.summary = "Print the HelloWorld"
spec.description = "Print the HelloWorld"
spec.homepage = "https://github.com/test/hello_world_gem"
spec.required_ruby_version = ">= 2.6.0"
spec.metadata["allowed_push_host"] = "https://rubygems.org"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/test/two_factor_auth_gem"
spec.metadata["changelog_uri"] = "https://github.com/test/two_factor_auth_gem/blob/main/CHANGELOG.md"

#Specify which file should be added to the gem when it is released.
#The 'git ls-files -z' loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(_dir_) do
    'git ls-files -z'.split("\x0").reject do |f|
        (File.expand_path(f) == _FILE_) || 
           f.start_with?(*%w[bin/ test/ spec/ featues/ .git .circleci appveyor Gemfile])
          end
    end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
end

 

Gem Build:

  • Build the Gem Package: 
  • Run : gem build hello_world_gem.gemspec
  • This command will create a .gem file in your directory, which is the packaged gem ready for distribution
gem build  hello_world_gem.gemspec 

 

  •  How to push Gem 

    Step 1: Create the account on the rubygem.org
    Step2: Navigate your gem directory
    Step 3: Run: gem signin Add your credentials and make sure show_dashboard is enable
    Step 4: Push : gem push hello_world_gem-0.1.0.gem

 

gem signin
Enter your RubyGems.org credentilas.
Don't have an account yet? Create one at https://rubygems.org/sign_up
Email: test@gmailccom
Password: 

 

 


swaz_ahmed

I am swaz_ahmed blogger on shadbox. I am influencer,content writer,author and publisher. Feel free to ask me any question and suggestions.



Comments



Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads