How to create a Gem file

1) bundle gem gemwork1

(My IDE has option for “Gem Application” and given the name “gemwork1” as the application name)

2) in ‘lib/gemwork1.rb’ add the functionality code


add the following code to already existing module “Gemwork1”. here we adding form validation for email field

def validates_email_address_of attr_name

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
self.validates_format_of attr_name,
:with => email_regex,
:message => “is not a valid email address”
To extend it with ActiveRecord::Base class add this too

require “gemwork1”
class ActiveRecord::Base
extend Gemwork1

where “gemwork1” is file name and “Gemwork1” is module name

3) build the gem:- gem build gemwork1.gemspec or gem build
4) to install it into your system:- rake install
5) to check the working

create a new rails project and add the following in your Gemfile:
gem ‘gemwork1’

Run bundle, generate a User scaffold with an email:string attribute and migrate the database. In your User model add a validation for a given email string

validates_email_address_of :email

Now start the server and try to create a new user with an email address like “foo”. Rails should output an validation error.

6) to publis the gem in RubyGems

want a account in during the push command it will ask email and password for sign in
gem push gemwork1-0.0.1.gem




This entry was posted in rails, ROR, ruby on rails, web development and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s