RubyGems入門

 RubyGems入門

リンカーズさんの社内LT会で発表した内容です。RubyGemsの仕組みと作り方について。

6ac7c50770603b53964d44db373e8e48?s=128

Shinichi Maeshima

December 10, 2018
Tweet

Transcript

  1. rubygemsೖ໳

  2. ΈΜͳgem޷͖Ͱ͢ΑͶ??

  3. gemΛ࢖͏ਓ͸ଟ͍͚Ͳ࡞Δਓ͸গͳ͍

  4. gem࡞ΔͷͬͯࢥͬͨΑΓ΋ ؆୯ ͳΜͰ͢Αͱ͍͏࿩Λ͠·͢

  5. ࡞Δલʹɺ·ͣ࢓૊ Έʹ͍ͭͯͬ͘͟Γ ղઆ͠·͢

  6. ͦ΋ͦ΋gemͬͯͲ͏͍͏࢓૊Ͱ ಈ͍͍ͯΔͷ https://github.com/rubygems/rubygems » ΈΜͳ͓ͳ͡Έgem installͳͲͷίϚϯυ » gemͷrequireͷڍಈΛఏڙ͍ͯ͠Δ

  7. rubygemsΛrequire͢Δͱɺ require͕ஔ͖׵ΘΔ 1.·ͣ͸ී௨ʹrequire($LOAD_PATHΛ୳͢) 2.ͳʹ΋ݟ͔ͭΒͳ͔ͬͨΒɺաڈʹgem installͨ͠gemΛ୳͠ ͯɺݟ͔ͭͬͨΒ$LOAD_PATHʹͦͷgemͷpathΛ௥Ճͯ͠ require͢Δ

  8. rubygems͸ࣗಈͰrequire͞ΕΔ » Ruby1.9͔Β » ࣗಈͰrequireͨ͘͠ͳ͍ͱ͖͸RubyͷϏϧυ࣌ʹexport RUBYOPT='--disable gems'͓ͯ͘͠ඞཁ͕͋ΔΒ͍͠

  9. Ͱ΋๻ͨͪΏΔ;Θ Rails࢖͍ͬͯ΄ͱ ΜͲgemΛrequire͠ ͳ͍ΑͶʁ

  10. Bundler

  11. Gemfile ྫ gem ‘rails’, ‘>~ 5.2.0’ gem 'bootsnap', '>= 1.1.0',

    require: false
  12. Bundler.setup && Bundler.require ͜ΕΒ͸Rails؀ڥΛϩʔυ͢Δͱ͖ʹඞ࣮ͣߦ͞ΕΔ # config/boot.rb require 'bundler/setup' # config/application.rb

    Bundler.require(*Rails.groups)
  13. Bundler.setup Gemfileʹఆٛ͞Ε͍ͯΔgemΛͥΜͿ$LOAD_PATHʹ௥Ճ͢Δ

  14. Bundler.require(*Rails.groups) Gemfileʹఆٛ͞Ε͍ͯΔgemΛrequire͢Δɻͨͩ͠ » Ҿ਺ͷgroupʹଐ͍ͯ͠ͳ͍gem͸require͠ͳ͍ » gem 'bootsnap', '>= 1.1.0', require:

    falseΈ͍ͨʹ require: falseͱͳ͍ͬͯΔgem͸require͠ͳ͍ » ։ൃ༻ͱ͔ςετ༻ͷgemͩͱҰ୴require: falseͱ͓͍ͯ͠ ͯɺಛఆͷίϚϯυͷͱ͖͚ͩ໌ࣔతʹrequire͢Δɺͱ͍͏gem ͕࣌ʑ͋Γ·͢
  15. ࣍ʹ࡞Γํ

  16. budler͕gemͷ౔୆࡞Γ༻ͷίϚ ϯυΛఏڙ͍ͯ͠Δ bundle gem gemͷ໊લ ͰOK

  17. ࢼ͠ʹhelloworld gemΛ࡞ͬͯΈ ·͢ bundle gem helloworld Creating gem 'helloworld'... MIT

    License enabled in config Code of conduct enabled in config create helloworld/Gemfile create helloworld/lib/helloworld.rb create helloworld/lib/helloworld/version.rb create helloworld/helloworld.gemspec create helloworld/Rakefile create helloworld/README.md create helloworld/bin/console create helloworld/bin/setup create helloworld/.gitignore create helloworld/.travis.yml create helloworld/.rspec create helloworld/spec/spec_helper.rb create helloworld/spec/helloworld_spec.rb create helloworld/LICENSE.txt create helloworld/CODE_OF_CONDUCT.md Initializing git repo in /Users/shinichi.maeshima/tmp/helloworld Gem 'helloworld' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html
  18. ͨ͘͞ΜϑΝΠϧ͕ੜ੒͞Εͨ . !"" CODE_OF_CONDUCT.md !"" Gemfile !"" LICENSE.txt !"" README.md

    !"" Rakefile !"" bin # !"" console # $"" setup !"" helloworld.gemspec !"" lib # !"" helloworld # # $"" version.rb # $"" helloworld.rb $"" spec !"" helloworld_spec.rb $"" spec_helper.rb
  19. ཈͓͑ͯ͘΂͖͸͜ͷ̎ͭ » lib/helloworld.rb » gemΛrequireͨ͠ͱ͖ͷى఺ʹͳΔϑΝΠϧ » helloworld.gemspec » gemͷઆ໌΍ґଘؔ܎Λఆٛ͢ΔϑΝΠϧ

  20. lib/helloworld.rb ྫ require "helloworld/version" module Helloworld def self.say puts 'hello

    world!' end end
  21. helloworld.gemspec ྫɻҰ෦লུͯ͠·͢ Gem::Specification.new do |spec| spec.name = "helloworld" spec.version =

    Helloworld::VERSION spec.authors = ["willnet"] spec.email = ["netwillnet@gmail.com"] spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.} spec.description = %q{TODO: Write a longer description or delete this line.} spec.homepage = "TODO: Put your gem's website or public repo URL here." spec.license = "MIT" spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.17" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" end
  22. TODO: ͱͳ͍ͬͯΔͱ͜ΖΛॻ͍ͯͳ͍ͱౖ ΒΕΔͷͰͪΌΜͱॻ͖·͠ΐ͏ ྫͷsummaryͱdescriptionͱhomepageΛमਖ਼ Gem::Specification.new do |spec| spec.name = "helloworld"

    spec.version = Helloworld::VERSION spec.authors = ["willnet"] spec.email = ["netwillnet@gmail.com"] spec.summary = %q{Hello World} spec.description = %q{Hello World gem for sample} spec.homepage = “https://github.com/willnet/helloworld” spec.license = "MIT" spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.17" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" end
  23. ґଘ͍ͯ͠Δgem͕͋Δͱ͖͸ helloworld.gemspecʹॻ͖ ·͢ ྫ spec.add_dependency ‘rails’ spec.add_development_dependency ‘gimei’

  24. ϩʔΧϧ؀ڥʹΠϯετʔϧ͠ ͯಈ࡞Λࢼ͍ͨ͠ rake install require 'helloworld' #=> true Helloworld.say #=>

    nil hello world!
  25. ςετͷৄࡉͳॻ͖ํ͸লུ͠ ·͢ ςετ༻ͷσΟϨΫτϦ͕ੜ੒͞Ε͍ͯΔͷͰɺ͙͢ʹ͍ͭ΋ͷΑ͏ʹॻ ͚·͢

  26. ςετॻ͖·͠ΐ͏ Ͷ ❤

  27. gem͕׬੒ͨ͠

  28. rubygems.orgʹϦϦʔε͢Δ ͧʂ ࣄલʹrubygems.orgͰΞΧ΢ϯτ࡞͓ͬͯ͘ඞཁ͋Δ͸ͣ rake release

  29. ࣾ಺Ͱ͔͠࢖Θͳ͍gemͬͯͲ͏ ϗεςΟϯά͢Δͷʁ » github΍gitlabʹίʔυΛϗεςΟϯά » Gemfileʹ࣍ͷΑ͏ʹॻ͍͓͚ͯ͹OK gem ‘helloworld’, git: ‘https://github.com/willnet/helloworld.git'

  30. gemʹ੾Γग़ͤͦ͏ ͳίʔυɺͲΜͲΜ ੾Γग़͍͖ͯ͠·͠ ΐ͏!!!1