Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cutting your own RubyGems

Cutting your own RubyGems

Diving head-first into RubyGems is rough: there’s a myriad of documentation sites, blog posts, screencasts, and manpages to read, and it’s tough to find “the” guide on how to publish your first RubyGem. In this session you’ll learn why making them is important for your Rails application’s maintainability and how distributing your code in gems even internally is helpful. You’ll find out what happens from “gem push” to “gem install”, and how Rails’ engine system makes sharing application and business logic easy.

Given at RailsConf 2011. http://en.oreilly.com/rails2011

Nick Quaranto

January 12, 2012
Tweet

More Decks by Nick Quaranto

Other Decks in Programming

Transcript

  1. % tree freewill freewill/ |-- bin/ | `-- freewill |--

    lib/ | `-- freewill.rb |-- test/ | `-- test_freewill.rb |-- README |-- Rakefile `-- freewill.gemspec Wednesday, May 25, 2011
  2. % irb -rpp >> require 'rake' LoadError: no such file

    to load -- rake from (irb):2:in `require' from (irb):2 Wednesday, May 25, 2011
  3. >> require 'rubygems' => true >> require 'rake' => true

    >> pp $LOAD_PATH[0..1] [".../gems/rake-0.8.7/bin", ".../gems/rake-0.8.7/lib"] Wednesday, May 25, 2011
  4. % cat freewill.gemspec Gem::Specification.new do |s| s.name = 'freewill' s.version

    = '1.0.0' s.date = '2010-04-27' s.summary = "Freewill!" s.description = "I will choose Freewill!" s.authors = ["Nick Quaranto"] s.email = '[email protected]' s.homepage = 'http://example.com' s.files = ["lib/freewill.rb"] end Wednesday, May 25, 2011
  5. % gem fetch rake Downloaded rake-0.8.7 % tar zxvf rake-0.8.7.gem

    x data.tar.gz x metadata.gz Wednesday, May 25, 2011
  6. % cat lib/hola.rb class Hola def self.hi(msg = "world") puts

    "Hello #{msg}!" end end Wednesday, May 25, 2011
  7. % cat hola.gemspec Gem::Specification.new do |s| s.name = 'hola' s.version

    = '0.0.0' s.date = '2010-04-28' s.summary = "Hola!" s.description = "A simple hello world gem" s.authors = ["Nick Quaranto"] s.email = '[email protected]' s.files = ["lib/hola.rb"] s.homepage = 'http://rubygems.org/gems/hola' end Wednesday, May 25, 2011
  8. % gem build hola.gemspec Successfully built RubyGem Name: hola Version:

    0.0.0 File: hola-0.0.0.gem % gem install ./hola-0.0.0.gem Successfully installed hola-0.0.0 1 gem installed Wednesday, May 25, 2011
  9. % irb -rubygems >> require 'hola' => true >> Hola.hi

    Hello world! Wednesday, May 25, 2011
  10. % irb -rubygems >> require 'hola' => true >> Hola.hi

    Hello world! require 'ubygems.rb' Wednesday, May 25, 2011
  11. % gem push hola-0.0.0.gem Enter your RubyGems.org credentials. Don't have

    an account yet? Create one at http://rubygems.org/sign_up Email: [email protected] Password: Signed in. Pushing gem to RubyGems.org... Successfully registered gem: hola (0.0.0) Wednesday, May 25, 2011
  12. % gem list -r hola *** REMOTE GEMS *** hola

    (0.0.0) % gem install hola Successfully installed hola-0.0.0 1 gem installed Wednesday, May 25, 2011
  13. % cat bin/hola #!/usr/bin/env ruby require 'hola' puts Hola.hi(ARGV[0]) “Probably

    derived from “shell bang” under the influence of American slang “the whole shebang” (everything, the works)” http://www.retrologic.com/jargon/S/shebang.html (shə-băng') Wednesday, May 25, 2011
  14. % cat bin/hola #!/usr/bin/env ruby require 'hola' puts Hola.hi(ARGV[0]) (shə-băng')

    “Probably derived from “shell bang” under the influence of American slang “the whole shebang” (everything, the works)” http://www.retrologic.com/jargon/S/shebang.html Wednesday, May 25, 2011
  15. % ruby -Ilib ./bin/hola Hello world! % ruby -Ilib ./bin/hola

    Baltimore Hello Baltimore! Wednesday, May 25, 2011
  16. % ruby -Ilib ./bin/hola Hello world! % ruby -Ilib ./bin/hola

    Baltimore Hello Baltimore! $LOAD_PATH.unshift(“lib”) Wednesday, May 25, 2011
  17. % head -4 hola.gemspec Gem::Specification.new do |s| s.name = 'hola'

    s.version = '0.0.1' s.executables << 'hola' Wednesday, May 25, 2011
  18. framework automation rake thor Test::Unit rspec bacon context matchy shoulda

    riot testy shindo zebra lemon dfect cucumber steak Wednesday, May 25, 2011
  19. % cat Rakefile require 'rake/testtask' Rake::TestTask.new do |t| t.libs <<

    'test' end desc "Run tests" task :default => :test Wednesday, May 25, 2011
  20. % cat test/test_hola.rb require 'test/unit' require 'hola' class HolaTest <

    Test::Unit::TestCase def test_default_hello assert_equal "Hello world!", Hola.hi end def test_custom_hello assert_equal "Hello Boston!", Hola.hi("Boston") end end Wednesday, May 25, 2011
  21. % rake test (in /Users/qrush/Dev/ruby/hola) Loaded suite Started .. Finished

    in 0.000736 seconds. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips Test run options: --seed 15331 Wednesday, May 25, 2011
  22. # The main Hola driver class Hola # Say hi

    to the world! # # Example: # >> Hola.hi("Buffalo") # => Hello Buffalo! # # Arguments: # message: (String) def self.hi(message = "world") puts "Hello #{message}!" end end Wednesday, May 25, 2011
  23. . └── lib ├── hola │ └── translator.rb └── hola.rb

    BAD: require File.join(File.dirname( __FILE__), "hola", "translator") Wednesday, May 25, 2011
  24. . └── lib ├── hola │ └── translator.rb └── hola.rb

    WORSE: $LOAD_PATH.unshift "lib/hola" require "translator" Wednesday, May 25, 2011
  25. . └── lib ├── hola │ └── translator.rb └── hola.rb

    GOOD: require "hola/translator" Wednesday, May 25, 2011
  26. . └── lib ├── foo │ └── cgi.rb ├── foo.rb

    └── set.rb require ‘set’ Wednesday, May 25, 2011
  27. . └── lib ├── foo │ └── cgi.rb ├── foo.rb

    └── set.rb require ‘foo/cgi’ Wednesday, May 25, 2011
  28. development runtime what your gem needs to work what your

    tests need to work Wednesday, May 25, 2011
  29. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "daemons", ["= 1.1.0"]) s.add_development_dependency( "rspec", [">= 2.2.0"]) Wednesday, May 25, 2011
  30. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "daemons", ["= 1.1.0"]) s.add_development_dependency( "rspec", [">= 2.0.0"]) Exact This one works for me! Wednesday, May 25, 2011
  31. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "daemons", ["= 1.1.0"]) s.add_development_dependency( "rspec", [">= 2.2.0"]) Optimistic It will always work! Wednesday, May 25, 2011
  32. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "daemons", ["= 1.1.0"]) s.add_development_dependency( "rspec", [">= 2.2.0", "< 3.0.0"]) Pessimistic This and future minor releases work Wednesday, May 25, 2011
  33. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "daemons", ["= 1.1.0"]) s.add_development_dependency( "rspec", ["~> 2.2"]) Pessimistic Use the twiddle-wakka! Wednesday, May 25, 2011
  34. T H X Thanks for listening! Big thanks to: thoughtbot

    Jeremy Hinegardner Nick Quaranto @qrush [email protected] http://guides.rubygems.org Wednesday, May 25, 2011