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

Writing Ruby Gems

Writing Ruby Gems

An introduction to how to write Ruby gems - particularly focusing on the gemspec, naming files, managing dependencies, publishing gems, and some bonus tips for writing gems that integrate with Rails.

If you want a version with speaker notes - or the video - visit https://freelancing-gods.com/slides/gems.html

Pat Allan

March 25, 2020
Tweet

More Decks by Pat Allan

Other Decks in Technology

Transcript

  1. To re-use code Why write a gem? To make code

    easy to install To make code easy to share
  2. Gem::Specification.new do |spec| spec.name = "melb_ruby" spec.version = "0.0.1" spec.authors

    = ["Pat Allan"] spec.email = ["[email protected]"] spec.summary = "A gem that does something" spec.homepage = "https://github.com/pat/melb_ruby" spec.license = "MIT" # ... end
  3. Gem::Specification.new do |spec| spec.name = "melb_ruby" spec.version = "0.0.1" spec.authors

    = ["Pat Allan"] spec.email = ["[email protected]"] spec.summary = "A gem that does something" spec.homepage = "https://github.com/pat/melb_ruby" spec.license = "MIT" # ... end
  4. Gem::Specification.new do |spec| spec.name = "melb_ruby" spec.version = "0.0.1" spec.authors

    = ["Pat Allan"] spec.email = ["[email protected]"] spec.summary = "A gem that does something" spec.homepage = "https://github.com/pat/melb_ruby" spec.license = "MIT" # ... end
  5. Gem::Specification.new do |spec| spec.name = "melb_ruby" spec.version = "0.0.1" spec.authors

    = ["Pat Allan"] spec.email = ["[email protected]"] spec.summary = "A gem that does something" spec.homepage = "https://github.com/pat/melb_ruby" spec.license = "MIT" # ... end
  6. Gem::Specification.new do |spec| spec.name = "melb_ruby" spec.version = "0.0.1" spec.authors

    = ["Pat Allan"] spec.email = ["[email protected]"] spec.summary = "A gem that does something" spec.homepage = "https://github.com/pat/melb_ruby" spec.license = "MIT" # ... end
  7. Gem::Specification.new do |spec| spec.name = "melb_ruby" spec.version = "0.0.1" spec.authors

    = ["Pat Allan"] spec.email = ["[email protected]"] spec.summary = "A gem that does something" spec.homepage = "https://github.com/pat/melb_ruby" spec.license = "MIT" # ... end
  8. Gem::Specification.new do |spec| spec.name = "melb_ruby" spec.version = "0.0.1" spec.authors

    = ["Pat Allan"] spec.email = ["[email protected]"] spec.summary = "A gem that does something" spec.homepage = "https://github.com/pat/melb_ruby" spec.license = "MIT" # ... end
  9. melb_ruby/lib # code goes here melb_ruby/exe # executables melb_ruby/bin #

    development commands melb_ruby/spec # or melb_ruby/test melb_ruby/README.markdown melb_ruby/CHANGELOG.markdown
  10. melb_ruby/lib # code goes here melb_ruby/exe # executables melb_ruby/bin #

    development commands melb_ruby/spec # or melb_ruby/test melb_ruby/README.markdown melb_ruby/CHANGELOG.markdown
  11. melb_ruby/lib # code goes here melb_ruby/exe # executables melb_ruby/bin #

    development commands melb_ruby/spec # or melb_ruby/test melb_ruby/README.markdown melb_ruby/CHANGELOG.markdown
  12. melb_ruby/lib # code goes here melb_ruby/exe # executables melb_ruby/bin #

    development commands melb_ruby/spec # or melb_ruby/test melb_ruby/README.markdown melb_ruby/CHANGELOG.markdown
  13. melb_ruby/lib # code goes here melb_ruby/exe # executables melb_ruby/bin #

    development commands melb_ruby/spec # or melb_ruby/test melb_ruby/README.markdown melb_ruby/CHANGELOG.markdown
  14. melb_ruby/lib # code goes here melb_ruby/exe # executables melb_ruby/bin #

    development commands melb_ruby/spec # or melb_ruby/test melb_ruby/README.markdown melb_ruby/CHANGELOG.markdown
  15. # A list of all files in the gem: spec.files

    = ["lib/melb_ruby.rb"] # This is the default: spec.require_paths = ["lib"] # This is *not* the default, but maybe should be: spec.bindir = "exe" # And then you need to explicitly list each # executable: spec.executables = ["melburnian"]
  16. # A list of all files in the gem: spec.files

    = ["lib/melb_ruby.rb"] # This is the default: spec.require_paths = ["lib"] # This is *not* the default, but maybe should be: spec.bindir = "exe" # And then you need to explicitly list each # executable: spec.executables = ["melburnian"]
  17. # A list of all files in the gem: spec.files

    = ["lib/melb_ruby.rb"] # This is the default: spec.require_paths = ["lib"] # This is *not* the default, but maybe should be: spec.bindir = "exe" # And then you need to explicitly list each # executable: spec.executables = ["melburnian"]
  18. # A list of all files in the gem: spec.files

    = ["lib/melb_ruby.rb"] # This is the default: spec.require_paths = ["lib"] # This is *not* the default, but maybe should be: spec.bindir = "exe" # And then you need to explicitly list each # executable: spec.executables = ["melburnian"]
  19. File structure should match the gem name Naming is hard

    Use underscores between words Use dashes for namespacing
  20. # don't put MelbRuby::Burgers in lib/burgers.rb # because a gem

    called Burgers # probably has this file too, and # Ruby will get confused.
  21. # lib/melb_ruby/railtie.rb class MelbRuby::Railtie < Rails::Railtie config.to_prepare do MelbRuby::Prepare.call end

    initializer 'melb_ruby.initialisation' do # ... end rake_tasks do load File.expand_path('../tasks.rb', __FILE__) end end