Another Japan in the World

Jun Aruga's blog.

Create own RubyGem

I changed my ruby scripts to gem package.


Module name is "recommendation_sample".
This is used below way in Ruby code.

require 'recommendation_sample'

See below URL about module name.

The difference of "_" and "-".
recommendation_sample => require 'recommendation_sample'
recommendation-sample=> require 'recommendation/sample'

Install bundler if it is not installed.

$ gem install bundler

$ gem list | grep bundler
bundler (1.11.2)

Create init files for gem.

$ bundle gem recommendation_sample -t

Below file is starting point in this case.

$ cd recommendation_sample
$ vi lib/recommendation_sample.rb

After writing the code.

Edit gemspec file.

$ vi recommendation_sample.gemspec
  spec.summary       = %q{your_summary}
  spec.description   = %q{your_description}
  spec.homepage      = "your_homepage_URL"

Test to run

Can install your local edited code as gem, by bundler install.

$ bundle install

$ bundle list | grep recommendation_sample
  * recommendation_sample (0.1.0)
$ bundle exec irb
irb(main):001:0> require 'recommendation_sample'
=> true
irb(main):002:0> r ='users')
=> #<RecommendationSample::Record:0x007fc55a28ae70 @table_name="users">
irb(main):003:0> r.find_one(2)
=> {"id"=>2, "sex"=>2, "hobby_ids"=>[2]}

Sample code is below URL (feature branch).
Later it will be merged to develop branch.