Many of us used of so many gems in ruby, so lets try to create a basic gem.
Simple, We just have to generate two files to create your own gem.
Step1: Create a gemspec.
Create a gemspec file inside root directory, file-name should be same as your gem name (Eg: In our case its greeting_gem.gemspec)
– Specify your gem specifications
1 2 3 4 5 6 7 8 9 |
Gem::Specification.new do |s| s.name = "greeting_gem" s.version = '0.0.0' s.date = '2013-11-28' s.summary = "Greeting Gem" s.description = "A simple greeting gem" s.authors = "Sravani Perala" s.files = "lib/greeting_gem.rb" end |
Step2: Add some code.
Create a greeting_gem.rb file in lib folder
1 2 3 4 5 |
class GreetingGem def self.greeting(name) puts "Helloo #{name}" end end |
Step 3: Generate gem file.
1 |
gem build greeting_gem.gemspec |
This command will build the gem, which can be used in another ruby program.
Step 4: Test the gem.
gem install greeting_gem, to install the gem locally to test it out.
open irb
>> require ‘greeting_gem’
=> true
>> GreetingGem.greeting(‘sravani’)
Hello sravani
Isn’t it simple!
Add as many methods you want in the same way and move on to bigger things.