Hi friends,
The popularity of Ruby on Rails now a days is so much because of its rapid development, which is possible because of its gems. For every generic functionality there is a gem available, so why not create our own gem. Today I am going to tell you how you can create your own gem and push it into ruby gems.
Lets have a simple example of creating a gem that outputs the fibonacci sequence of a number. The steps that will be followed are.
Step 1: Name your gem
First step is to give your gem a name in my case I am giving it a name as fibonacci_raghvendra1501
Step 2: Structuring
The structure for the gem is like this:
fibonacci_raghvendra1501.gemspec
lib
fibonacci_raghvendra1501.rb
The main code of this package is placed into the lib directory. There should be one file in the lib with the name of the gem so that it can be required.
Step 3: Code
Now add your code into the file, in our case I am adding the simple function that return the fibonacci of a number into fibonacci_raghvendra1501.rb
class FibonacciRaghvendra1501
def self.fibonacciSeq(n)
a = [0]
n.times do |i|
if i==0
a[i] = 0
elsif i==1
a[i] = 1
else
a[i] = a[i-1] + a[i-2]
end
end
return a[n-1]
end
end
Step 4: Add information into gemspec
The gemspec provides the information about the gem, its author, version. All the information that is showed into the gem page are given here. A sample gemspec is as follows:
Gem::Specification.new do |s|
s.name = 'fibonacci_raghvendra1501'
s.version = '0.0.0'
s.date = '2016-04-05'
s.summary = "Fibonacci!"
s.description = "A simple fibonacci gem to get the fibonacci sequence of a number"
s.authors = ["Raghvendra"]
s.email = 'raghvendra1501@gmail.com'
s.files = ["lib/fibonacci_raghvendra1501.rb"]
s.homepage =
'http://rubygems.org/gems/fibonacci_raghvendra1501'
s.license = 'MIT'
end
Step 5: Installing the Gem
Now you need to install this gem to use. For that you need to build this gem. Then you can install it and use it:
> gem build fibonacci_raghvendra1501.gemspec
# => Output
Successfully built RubyGem
Name: fibonacci_raghvendra1501
Version: 0.0.0
File: fibonacci_raghvendra1501-0.0.0.gem
For installing this gem locally for testing you need to install it by:
> gem install ./fibonacci_raghvendra1501-0.0.0.gem
# => Output
Successfully installed fibonacci_raghvendra1501-0.0.0
Parsing documentation for fibonacci_raghvendra1501-0.0.0
Installing ri documentation for fibonacci_raghvendra1501-0.0.0
Done installing documentation for fibonacci_raghvendra1501 after 0 seconds
1 gem installed
Step 6: Using the Gem Now at last you need to use this gem by requiring it to a file or anything like that
> irb
>> require "fibonacci_raghvendra1501"
=> true
>> Hola.fibonacciSeq(5)
=> 3
Step 7: Pushing gem to the Ruby gem community Now at last, to push the gem to the rubygems community, all you need to do this is setup the system with credentials to ruby gem and then a single line command to push it.
> curl -u your_username https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
> Enter host password for user 'your_username':
> gem push fibonacci_raghvendra1501-0.0.0.gem
#=> Output
Pushing gem to https://rubygems.org...
Successfully registered gem: fibonacci_raghvendra1501 (0.0.0)
Thus your gem is now available in https://rubygems.org/. Now just have fun with gems you create. For more blogs like this Click here.
0 Comment(s)