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

Becoming a Ruby Gemcutter

Becoming a Ruby Gemcutter

Welcome to the world of Ruby: where the gems are deep, rough, and rarely documented! No need to fear though, in this presentation you'll learn what a RubyGem is, how they're used in Rails and Ruby applications, and of course, how to make your own. You'll see just how easy it is to share your code with others in the Ruby universe, and why creating packages is a joy instead of a burden thanks to the tools and ecosystem of RubyGems.

Given at CodeMash 2012. http://codemash.org

Nick Quaranto

January 12, 2012
Tweet

More Decks by Nick Quaranto

Other Decks in Programming

Transcript

  1. YOU MUST BE THIS CODER TO RIDE THE TALK KNOW

    A BIT OF RUBY LOVE YOUR CODE WILLING TO LEARN!
  2. % tree freewill freewill/ |-- bin/ | `-- freewill |--

    lib/ | `-- freewill.rb |-- test/ | `-- test_freewill.rb |-- README |-- Rakefile `-- freewill.gemspec
  3. % tree freewill freewill/ |-- bin/ | `-- freewill |--

    lib/ | `-- freewill.rb |-- test/ | `-- test_freewill.rb |-- README |-- Rakefile `-- freewill.gemspec DOCS!
  4. % tree freewill freewill/ |-- bin/ | `-- freewill |--

    lib/ | `-- freewill.rb |-- test/ | `-- test_freewill.rb |-- README |-- Rakefile `-- freewill.gemspec CODE!
  5. % tree freewill freewill/ |-- bin/ | `-- freewill |--

    lib/ | `-- freewill.rb |-- test/ | `-- test_freewill.rb |-- README |-- Rakefile `-- freewill.gemspec GEMSPEC!
  6. % irb -rpp >> require 'rake' LoadError: no such file

    to load -- rake from (irb):2:in `require' from (irb):2
  7. >> require 'rubygems' => true >> require 'rake' => true

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

    = '1.0.0' s.date = '2012-01-03' 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
  9. NO REALLY WHY SHOULD I BOTHER WITH THIS, I’M BUSY!

    REUSE YOUR CODE EASY TO INSTALL EASY TO SHARE
  10. % cat hola.gemspec Gem::Specification.new do |s| s.name = 'hola' s.version

    = '0.0.0' s.date = '2011-01-03' 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
  11. % 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
  12. % 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)
  13. % gem list -r hola *** REMOTE GEMS *** hola

    (0.0.0) % gem install hola Successfully installed hola-0.0.0 1 gem installed
  14. % 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')
  15. % 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
  16. % ruby -Ilib ./bin/hola Hello world! % ruby -Ilib ./bin/hola

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

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

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

    'test' end desc "Run tests" task :default => :test
  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
  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
  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
  23. . #"" lib !"" hola % #"" translator.rb #"" hola.rb

    BAD: require File.join(File.dirname( __FILE__), "hola", "translator")
  24. . #"" lib !"" hola % #"" translator.rb #"" hola.rb

    WORSE: $LOAD_PATH.unshift "lib/hola" require "translator"
  25. . #"" lib !"" hola % #"" translator.rb #"" hola.rb

    GOOD: require "hola/translator"
  26. 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"])
  27. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "thor", [">= 0.14.5"]) s.add_development_dependency( "rspec", ["= 2.2.0"]) Only this one works for me! EXACT
  28. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "thor", [">= 0.14.5"]) s.add_development_dependency( "rspec", ["= 2.2.0"]) It will always work! OPTIMISTIC
  29. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "thor", [">= 0.14.5", "< 1.0.0"]) s.add_development_dependency( "rspec", ["= 2.2.0"]) Lock to a range instead PESSIMISTIC
  30. Gem::Specification.new do |s| s.name = "hola" s.version = "2.0.0" s.add_runtime_dependency(

    "thor", ["~> 0.14"]) s.add_development_dependency( "rspec", ["= 2.2.0"]) Use the twiddle-wakka! PESSIMISTIC