how to make a ruby gem - austin on rails, january 2014

97
How to Make a Ruby Gem Clare Glinka 1

Upload: clare-glinka

Post on 15-Jan-2015

945 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: How to make a Ruby Gem - Austin on Rails, January 2014

How to Make a Ruby Gem

Clare Glinka

1

Page 2: How to make a Ruby Gem - Austin on Rails, January 2014

Who is this talk for?•Me, a few months ago

•Someone who is:

•new to Rails

•new to Ruby

•new to programming

2

Page 3: How to make a Ruby Gem - Austin on Rails, January 2014

What is a gem?

• Container that hold widely-reusable code.

• Code that is useable in many different contexts.

3

Page 4: How to make a Ruby Gem - Austin on Rails, January 2014

Tools to help with making gems:•Bundler•Jeweler•gemcutter•others!

4

Page 5: How to make a Ruby Gem - Austin on Rails, January 2014

Building a Gem <that generates cheesey adventure plots>

with Bundler

5

Page 6: How to make a Ruby Gem - Austin on Rails, January 2014

Step 0.5: $ gem install bundler

6

Page 7: How to make a Ruby Gem - Austin on Rails, January 2014

Step 1: $ bundle gem adventure

7

Page 8: How to make a Ruby Gem - Austin on Rails, January 2014

=> create adventure/Gemfile create adventure/Rakefile create adventure/LICENSE.txt create adventure/README.md create adventure/.gitignore create adventure/adventure.gemspec create adventure/lib/adventure.rb create adventure/lib/adventure/version.rbInitializing git repo in /Users/me/code/adventure

8

Page 9: How to make a Ruby Gem - Austin on Rails, January 2014

Gem structureadventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec└── lib ├── adventure.rb └── adventure └── version.rb

9

Page 10: How to make a Ruby Gem - Austin on Rails, January 2014

Step 3: Finish .gemspec

10

Page 11: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec└── lib ├── adventure.rb └── adventure └── version.rb

11

Page 12: How to make a Ruby Gem - Austin on Rails, January 2014

adventure.gemspeclib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = %q{TODO: Write a short summary. Required.} spec.description = %q{TODO: Write a longer description. Optional.} spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

12

Page 13: How to make a Ruby Gem - Austin on Rails, January 2014

Required specs•name•version•summary•require_paths•files•rubygems_version

13

Page 14: How to make a Ruby Gem - Austin on Rails, January 2014

spec.name

14

Page 15: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = %q{TODO: Write a short summary. Required.} spec.description = %q{TODO: Write a longer description. Optional.} spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

15

Page 16: How to make a Ruby Gem - Austin on Rails, January 2014

spec.version

16

Page 17: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = %q{TODO: Write a short summary. Required.} spec.description = %q{TODO: Write a longer description. Optional.} spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

17

Page 18: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec└── lib ├── adventure.rb └── adventure └── version.rb

18

Page 19: How to make a Ruby Gem - Austin on Rails, January 2014

module Adventure VERSION = "0.0.1"end

lib/adventure/version.rb

19

Page 20: How to make a Ruby Gem - Austin on Rails, January 2014

Versioning• Use semantic versioning.

• 0 . 0 . X - bugfixs

• 0 . X . 0- additional functionality that is backward compatible.

• X . 0 . 0- additional functionality that breaks backwards compatibility.

20

Page 21: How to make a Ruby Gem - Austin on Rails, January 2014

Version constraints• ~> 1.2

- all versions before 2 . 0

• ~> 1 . 3 . 5

- all versions before 1. 4 . 0

21

Page 22: How to make a Ruby Gem - Austin on Rails, January 2014

spec.summary

22

Page 23: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = %q{TODO: Write a short summary. Required.} spec.description = %q{TODO: Write a longer description. Optional.} spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

23

Page 24: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

24

Page 25: How to make a Ruby Gem - Austin on Rails, January 2014

spec.require_paths

25

Page 26: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

26

Page 27: How to make a Ruby Gem - Austin on Rails, January 2014

Protect the load path!$ pry

[1] pry(main)> $LOAD_PATH

=> ["/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/coderay-1.1.0/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/slop-3.4.7/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/method_source-0.8.2/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/pry-0.9.12.4/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/x86_64-darwin13.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/site_ruby", "/rbenv/versions/2.0.0-p353/lib/ruby/vendor_ruby/2.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/vendor_ruby/2.0.0/x86_64-darwin13.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/vendor_ruby", "/rbenv/versions/2.0.0-p353/lib/ruby/2.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin13.0.0"]

27

Page 28: How to make a Ruby Gem - Austin on Rails, January 2014

Protect the load path![2] pry(main)> require 'adventure'=> true

[3] pry(main)> $LOAD_PATH=> ["/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/coderay-1.1.0/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/slop-3.4.7/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/method_source-0.8.2/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/pry-0.9.12.4/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/adventure-0.0.1/lib", "/rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/site_ruby/2.0.0/x86_64-darwin13.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/site_ruby", "/rbenv/versions/2.0.0-p353/lib/ruby/vendor_ruby/2.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/vendor_ruby/2.0.0/x86_64-darwin13.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/vendor_ruby", "/rbenv/versions/2.0.0-p353/lib/ruby/2.0.0", "/rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin13.0.0"]

28

Page 29: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec└── lib ├── adventure.rb └── adventure └── version.rb

29

Page 30: How to make a Ruby Gem - Austin on Rails, January 2014

spec.files

30

Page 31: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

31

Page 32: How to make a Ruby Gem - Austin on Rails, January 2014

$ git ls-files

.gitignoreGemfileLICENSE.txtREADME.mdRakefileadventure.gemspeclib/adventure.rblib/adventure/version.rb

32

Page 33: How to make a Ruby Gem - Austin on Rails, January 2014

$  pry

[1]  pry(main)>  `git  ls-­‐files`=>  ".gitignore\nGemfile\nLICENSE.txt\nREADME.md\nRakefile\nadventure.gemspec\nlib/adventure.rb\nlib/adventure/version.rb\n"[2]  pry(main)>  `git  ls-­‐files`.split($\)=>  [".gitignore",  "Gemfile",  "LICENSE.txt",  "README.md",  "Rakefile",  "adventure.gemspec",  "lib/adventure.rb",  "lib/adventure/version.rb"]

33

Page 34: How to make a Ruby Gem - Austin on Rails, January 2014

$  pry

[1]  pry(main)>  `git  ls-­‐files`=>  ".gitignore\nGemfile\nLICENSE.txt\nREADME.md\nRakefile\nadventure.gemspec\nlib/adventure.rb\nlib/adventure/version.rb\n"[2]  pry(main)>  `git  ls-­‐files`.split($\)=>  [".gitignore",  "Gemfile",  "LICENSE.txt",  "README.md",  "Rakefile",  "adventure.gemspec",  "lib/adventure.rb",  "lib/adventure/version.rb"]

34

Page 35: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "" spec.license = "MIT"

spec.files = [".gitignore", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "adventure.gemspec", "lib/adventure.rb", "lib/adventure/version.rb"] spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

35

Page 36: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

36

Page 37: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "" spec.license = "MIT"

spec.files = Dir.glob(‘**/*’) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

37

Page 38: How to make a Ruby Gem - Austin on Rails, January 2014

$  pry

[1]  pry(main)>  Dir.glob('**/**')=>  ["adventure.gemspec",  "Gemfile",  "lib",  "lib/adventure",  "lib/adventure/random.txt",  "lib/adventure/version.rb",  "lib/adventure.rb",  "LICENSE.txt",  "Rakefile",  "README.md"]

38

Page 39: How to make a Ruby Gem - Austin on Rails, January 2014

spec.rubygems_version

39

Page 40: How to make a Ruby Gem - Austin on Rails, January 2014

Other Specs:•authors•email•homepage• license•executables•test_files•add_development_dependency•add_dependency

40

Page 41: How to make a Ruby Gem - Austin on Rails, January 2014

spec.authorsspec.email

spec.homepage

41

Page 42: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

42

Page 43: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "http://www.github.com/cglinka/adventure" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

43

Page 44: How to make a Ruby Gem - Austin on Rails, January 2014

spec.license

44

Page 45: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "http://www.github.com/cglinka/adventure" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

45

Page 46: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec└── lib ├── adventure.rb └── adventure └── version.rb

46

Page 47: How to make a Ruby Gem - Austin on Rails, January 2014

spec.executablesspec.test_files

47

Page 48: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "http://github.com/cglinka/adventure" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

48

Page 49: How to make a Ruby Gem - Austin on Rails, January 2014

Step 4: Write some code!

49

Page 50: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec└── lib ├── adventure.rb └── adventure └── version.rb

50

Page 51: How to make a Ruby Gem - Austin on Rails, January 2014

require  "adventure/version"

module  Adventure    #  Your  code  goes  here...end

51

Page 52: How to make a Ruby Gem - Austin on Rails, January 2014

“There is a disgruntled shopkeeper with a secret past and a grad student with a knack for cooking.

They fight crime!”

Desired output:

52

Page 53: How to make a Ruby Gem - Austin on Rails, January 2014

require  "adventure/version"

module  Adventure    class  Plot        def  make_plot            characters  =  ["a  clever  dragon",                                          "a  ruthless  detective",                                          "a  disgruntled  shopkeeper",                                          "an  aging  cowboy",                                          "a  grad  student",                                          "a  mystical  lizard"]            with  =  ["nothing  left  to  loose",                            "a  secret  past",                              "an  enchanted  sword",                              "a  grudge",                              "a  knack  for  cooking"]            who  =  ["likes  kebabs",                            "organizes  poetry  nights",                            "is  a  world-­‐famous  cyclist",                            "lived  in  a  circus"]

           "There  is  #{characters.sample}  with  #{with.sample}  and  #{characters.sample}  who  #{who.sample}.  They  fight  crime!"        end    endend

53

Page 54: How to make a Ruby Gem - Austin on Rails, January 2014

Step 5: build and install the gem

54

Page 55: How to make a Ruby Gem - Austin on Rails, January 2014

$ gem build adventure.gemspec

WARNING: no description specified Successfully built RubyGem Name: adventure Version: 0.0.1 File: adventure-0.0.1.gem

55

Page 56: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec├── adventure-0.0.1.gem└── lib ├── adventure.rb └── adventure └── version.rb

56

Page 57: How to make a Ruby Gem - Austin on Rails, January 2014

$ gem install adventure-0.0.1.gem

Successfully installed adventure-0.0.11 gem installed

57

Page 58: How to make a Ruby Gem - Austin on Rails, January 2014

$  pry

[1]  pry(main)>  require  'adventure'=>  true[2]  pry(main)>  plot  =  Adventure::Plot.new=>  #<Adventure::Plot:0x007fe2a4106498>[3]  pry(main)>  plot.make_plot=>  "There  is  a  grad  student  with  a  knack  for  cooking  and  a  ruthless  detective  who  likes  kebabs.  They  fight  crime!"

58

Page 59: How to make a Ruby Gem - Austin on Rails, January 2014

The easier way... Rake tasks!

59

Page 60: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "http://github.com/cglinka/adventure" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

60

Page 61: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec└── lib ├── adventure.rb └── adventure └── version.rb

61

Page 62: How to make a Ruby Gem - Austin on Rails, January 2014

source  'https://rubygems.org'

#  Specify  your  gem's  dependencies  in  adventure.gemspecgemspec

Gemfile

62

Page 63: How to make a Ruby Gem - Austin on Rails, January 2014

Rakefile

require  "bundler/gem_tasks"

63

Page 64: How to make a Ruby Gem - Austin on Rails, January 2014

$  rake  -­‐T

rake  build        #  Build  adventure-­‐0.0.1.gem  into  the  pkg  directory.rake  install    #  Build  and  install  adventure-­‐0.0.1.gem  into  system  gems.rake  release    #  Create  tag  v0.0.1  and  build  and  push  adventure-­‐0.0.1.gem  to  Rubygems

64

Page 65: How to make a Ruby Gem - Austin on Rails, January 2014

$  rake  build

adventure  0.0.1  built  to  pkg/adventure-­‐0.0.1.gem.

65

Page 66: How to make a Ruby Gem - Austin on Rails, January 2014

$  rake  install

adventure  0.0.1  built  to  pkg/adventure-­‐0.0.1.gem.adventure  (0.0.1)  installed.

66

Page 67: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├──.gitignore├── adventure.gemspec├── pkg│ └── adventure-0.0.1.gem└── lib ├── adventure.rb └── adventure └── version.rb

67

Page 68: How to make a Ruby Gem - Austin on Rails, January 2014

Step 6: Release the gem!

68

Page 69: How to make a Ruby Gem - Austin on Rails, January 2014

69

Page 70: How to make a Ruby Gem - Austin on Rails, January 2014

$  rake  release

70

Page 71: How to make a Ruby Gem - Austin on Rails, January 2014

And if everything has gone terribly wrong...

$ gem yank

*** Use with caution71

Page 72: How to make a Ruby Gem - Austin on Rails, January 2014

No More Hardcoding!

72

Page 73: How to make a Ruby Gem - Austin on Rails, January 2014

adventure.json{        "characters":  [                "a  clever  dragon",                "a  ruthless  detective",                "a  disgruntled  shopkeeper",                "an  aging  cowboy",                "a  grad  student",                "a  mystical  lizard"        ],        "with":  [                "nothing  left  to  loose",                "a  secret  past",                "an  enchanted  sword",                "a  grudge",                "a  knack  for  cooking"        ],        "who":  [                "likes  kebabs",                "organizes  poetry  nights",                "is  a  world-­‐famous  cyclist",                "lived  in  a  circus"        ]}

73

Page 74: How to make a Ruby Gem - Austin on Rails, January 2014

spec.add_dependency

74

Page 75: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "http://github.com/cglinka/adventure" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_dependency "json"

spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependency "rake"end

75

Page 76: How to make a Ruby Gem - Austin on Rails, January 2014

$ bundle install

Resolving dependencies...Using bundler (1.5.2)Using json (1.7.7)Using rake (0.9.6)Using adventure (0.0.1) from source at .Your bundle is complete!

76

Page 77: How to make a Ruby Gem - Austin on Rails, January 2014

require  "adventure/version"

module  Adventure    class  Plot        def  make_plot            characters  =  ["a  clever  dragon",                                          "a  ruthless  detective",                                          "a  disgruntled  shopkeeper",                                          "an  aging  cowboy",                                          "a  grad  student",                                          "a  mystical  lizard"]            with  =  ["nothing  left  to  loose",                            "a  secret  past",                              "an  enchanted  sword",                              "a  grudge",                              "a  knack  for  cooking"]            who  =  ["likes  kebabs",                            "organizes  poetry  nights",                            "is  a  world-­‐famous  cyclist",                            "lived  in  a  circus"]

           "There  is  #{characters.sample}  with  #{with.sample}  and  #{characters.sample}  who  #{who.sample}.  They  fight  crime!"        end    endend

77

Page 78: How to make a Ruby Gem - Austin on Rails, January 2014

require  "adventure/version"require  "json"

module  Adventure    class  Plot        def  make_plot(plotdevice)            plotholes  =  JSON.load(File.new(plotdevice))

           characters  =  plotholes["characters"]            with              =  plotholes["with"]            who                =  plotholes["who"]

           "There  is  #{characters.sample}  with  #{with.sample}  and  #{characters.sample}  who  #{who.sample}.  They  fight  crime!"        end    endend

lib/adventure.rb

78

Page 79: How to make a Ruby Gem - Austin on Rails, January 2014

$  rake  install

adventure  0.0.1  built  to  pkg/adventure-­‐0.0.1.gem.adventure  (0.0.1)  installed.

79

Page 80: How to make a Ruby Gem - Austin on Rails, January 2014

module Adventure VERSION = "0.0.1"end

lib/adventure/version.rb

80

Page 81: How to make a Ruby Gem - Austin on Rails, January 2014

module Adventure VERSION = "1.0.0"end

lib/adventure/version.rb

81

Page 82: How to make a Ruby Gem - Austin on Rails, January 2014

$  rake  install

adventure  1.0.0  built  to  pkg/adventure-­‐1.0.0.gem.adventure  (1.0.0)  installed.

82

Page 83: How to make a Ruby Gem - Austin on Rails, January 2014

$  pry

[1]  pry(main)>  require  'adventure'=>  true[2]  pry(main)>  plot  =  Adventure::Plot.new=>  #<Adventure::Plot:0x007fc079139e80>[3]  pry(main)>  plot.make_plot('adventure.json')=>  "There  is  a  clever  dragon  with  a  secret  past  and  a  clever  dragon  who  is  a  world-­‐famous  cyclist.  They  fight  crime!"

83

Page 84: How to make a Ruby Gem - Austin on Rails, January 2014

Set Up Testing with <RSpec>

84

Page 85: How to make a Ruby Gem - Austin on Rails, January 2014

# coding: utf-8lib = File.expand_path('../lib', __FILE__)$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)require 'adventure/version'

Gem::Specification.new do |spec| spec.name = "adventure" spec.version = Adventure::VERSION spec.authors = ["Clare Glinka"] spec.email = ["[email protected]"] spec.summary = "Adventure plot generator" spec.description = "" spec.homepage = "http://github.com/cglinka/adventure" spec.license = "MIT"

spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"]

spec.add_dependency "json" spec.add_development_dependency "bundler", "~> 1.4" spec.add_development_dependencyependency "rake" spec.add_development_dependencyependency "rspec"end

85

Page 86: How to make a Ruby Gem - Austin on Rails, January 2014

$ rspec --init

create spec/spec_helper.rb create .rspec

86

Page 87: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├── .gitignore├── .rspec├── adventure.gemspec├── pkg├── lib└── spec └── spec_helper.rb

87

Page 88: How to make a Ruby Gem - Austin on Rails, January 2014

spec/spec_helper.rb

#  This  file  was  generated  by  the  `rspec  -­‐-­‐init`  command.  Conventionally,  all#  specs  live  under  a  `spec`  directory,  which  RSpec  adds  to  the  `$LOAD_PATH`.#  Require  this  file  using  `require  "spec_helper"`  to  ensure  that  it  is  only#  loaded  once.##  See  http://rubydoc.info/gems/rspec-­‐core/RSpec/Core/ConfigurationRSpec.configure  do  |config|    config.treat_symbols_as_metadata_keys_with_true_values  =  true    config.run_all_when_everything_filtered  =  true    config.filter_run  :focus

   #  Run  specs  in  random  order  to  surface  order  dependencies.  If  you  find  an    #  order  dependency  and  want  to  debug  it,  you  can  fix  the  order  by  providing    #  the  seed,  which  is  printed  after  each  run.    #          -­‐-­‐seed  1234    config.order  =  'random'end

88

Page 89: How to make a Ruby Gem - Austin on Rails, January 2014

spec/spec_helper.rbrequire  'adventure'

#  This  file  was  generated  by  the  `rspec  -­‐-­‐init`  command.  Conventionally,  all#  specs  live  under  a  `spec`  directory,  which  RSpec  adds  to  the  `$LOAD_PATH`.#  Require  this  file  using  `require  "spec_helper"`  to  ensure  that  it  is  only#  loaded  once.##  See  http://rubydoc.info/gems/rspec-­‐core/RSpec/Core/ConfigurationRSpec.configure  do  |config|    config.treat_symbols_as_metadata_keys_with_true_values  =  true    config.run_all_when_everything_filtered  =  true    config.filter_run  :focus

   #  Run  specs  in  random  order  to  surface  order  dependencies.  If  you  find  an    #  order  dependency  and  want  to  debug  it,  you  can  fix  the  order  by  providing    #  the  seed,  which  is  printed  after  each  run.    #          -­‐-­‐seed  1234    config.order  =  'random'end

89

Page 90: How to make a Ruby Gem - Austin on Rails, January 2014

adventure├── Gemfile├── Rakefile├── LICENSE.txt├── README.md├── .gitignore├── .rspec├── adventure.gemspec├── pkg├── lib└── spec ├── spec_adventure.rb └── spec_helper.rb

90

Page 91: How to make a Ruby Gem - Austin on Rails, January 2014

spec/adventure_spec.rb

require  'spec_helper'

describe  Adventure::Plot  do    describe  "#make_plot"  do        it  "does  something"  do

       end    endend

91

Page 92: How to make a Ruby Gem - Austin on Rails, January 2014

$ bundle exec rspec

Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}.

Finished in 0.00052 seconds1 example, 0 failures

Randomized with seed 38431

92

Page 93: How to make a Ruby Gem - Austin on Rails, January 2014

Rakefile

require  "bundler/gem_tasks"require  "rspec/core/rake_task"

RSpec::Core::RakeTask.new('spec')

93

Page 94: How to make a Ruby Gem - Austin on Rails, January 2014

Rakefile

require  "bundler/gem_tasks"require  "rspec/core/rake_task"

RSpec::Core::RakeTask.new('spec')

94

Page 95: How to make a Ruby Gem - Austin on Rails, January 2014

$ rake spec

Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}.

Finished in 0.00052 seconds1 example, 0 failures

Randomized with seed 38431

95

Page 96: How to make a Ruby Gem - Austin on Rails, January 2014

96

Page 97: How to make a Ruby Gem - Austin on Rails, January 2014

Resources

guides.rubygems.org

link to slides @ClareGlinka_

97