SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
How to Write a Gem for Beginners
                     Ken C oar
Disclaimer
●   Don't be confused by the session title; this isn't
           –   How to write a 'gem for beginners'
●   It's
           –   'How to write a gem' for beginners
●   Also, Ken (that's me) doesn't profess to be an
    expert at any of this; he's just sharing
    experiences of what worked
Why?
●   You've seen all the Ruby gems available, and
    heard a lot about them. Where do they come
    from? How do you create one? And why would
    you want to?
●   There is no One True Way to create a gem, and
    different people will give you different
    recommendations.
●   This session describes one of the many paths
    available from 'why?' to 'ta-daah!' It worked for
    me; it might work for you too.
Pick a Licence
●   Why? Well, because Ruby is an interpreter,
    and offering up a gem to the chaos of the
    Internet is essentially opening the source code
    to the Universe
●   Do you care how your code may be used? To
    save lives? To crack banks? To guide
    missiles? To run gigantic financial institutions?
    To run schools? Think about it.
●   If you really don't care at all, maybe putting it in
    the public domain is the answer.
Source Control
●   You're gonna make mistakes, and want to back
    away from them – possibly hurriedly
●   You might want to have co-developers
●   Git is currently very popular with the Ruby
    crowd; after an initial bad reaction (because of
    a basic misunderstanding I had about it), I've
    come to prefer it myself
●   There are also Subversion, CVS, Arch,
    BitKeeper, and who-knows-what other options
●   You're going to want one, think about it up front
Disorder of Battle
●   These are not necessarily in order:
       –   Set up source control
       –   Set up the directory tree
       –   Write tests
       –   Learn Rake (if you haven't yet)
       –   Document, document, document
       –   Build
       –   Upload
Our victim: BitString
●   I'm going to use one of my own gems as an
    example, pretending to create it anew.
Creating the layout: newgem
●   I've got a lot of projects up in the air; as a
    consequence, I like to prefix the directories for
    my gems with 'rubygem-'
●   That doesn't play well with newgem, though,
    so we'll work around it later
●   Here we go:
        –
            newgem bitstring
            cp erehwon/LICENCE.txt bitstring/
What newgem did


  History.txt
  lib/
  LICENCE.txt
  Manifest.txt
  PostInstall.txt
  Rakefile
  README.rdoc
  script/
  test/
Set up source control
mv bitstring rubygem-bitstring
cd rubygem-bitstring
git init
git add *
echo '*~' >>.gitignore
git add .gitignore
git commit -m 'Initial commit'
Write code!
●   Now's the time to start actually writing the code.
●   Or, if it's already written, copying it into the gem
    directory tree.
●   Code goes under the 'lib/' subdirectory
●   I've already got mine, so I just copy it in
The lib/ tree

$ tree lib
lib
├── bitstring
│   └── operators.rb
└── bitstring.rb

1 directory, 2 files
Now you've got your code. Does it
             work?
●   Time to write tests. newgem started us off:
              $ tree test
              test
              ├── test_bitstring.rb
              └── test_helper.rb
              $ cat test/test_bitstring.rb
              require File.dirname(__FILE__) + '/test_helper.rb'

              class TestRubygemBitstring < Test::Unit::TestCase

               def setup
               end

               def test_truth
                assert true
               end
              end
Documentation (rdoc)
●   Okey, so your code works. You've tested it
    nigh unto death, and sent it off to the showers
    to recover.
●   But wait! There's more! While it's in the
    shower, you need to bring in a tattoo artist to..
●   Okey, that analogy isn't going anywhere useful.
    Try this:
        –   Add the internal rdoc documentation
●   Build the docs with rake docs and look at them
    (on your local filesystem) in your browser
The spec file
●   The .gemspec file is what tells the gem build
    everything it needs to know
●   Gemspecs can be complicated; fortunately,
    there's a shortcut to get you started:
       –   rake gemspec
●   After that, look at the foo.gemspec file and
    read up on gemspecs to see what changes you
    want/need to make
Building your gem
●   Once your code is written, your tests pass, your
    documentation is written (and verified), and
    your gemspec file has been created, you can
    actually build your gem!
         $ gem build bitstring.gemspec
          Successfully built RubyGem
          Name: bitstring
          Version: 1.0.0
          File: bitstring-1.0.0.gem

●   The name comes from the version and other
    details in the gemspec file
Uploading
●   Very simple!
       –   gem push bitstring-1.0.0.gem
●   Well, you need to have an account on the
    system first. Get one at
       –   http://rubygems.org/sign_up
Monitor on RubyGems
●   Once your gem has been uploaded, you can
    see statistics about its usage at
       –   https://rubygems.org/gems/yourgem
Notes and hints
●   Look at other gems
●   So far, this has all been done in your local
    environment. Once you feel confident, consider
    creating a project for your gem at RubyForge
    and uploading your code there.
       –   I highly recommend it!
Wrapup
●   Questions? Sure. Answers? Maybe. :-)

Mais conteúdo relacionado

Semelhante a Writing a Ruby Gem for beginners

Groovy there's a docker in my application pipeline
Groovy there's a docker in my application pipelineGroovy there's a docker in my application pipeline
Groovy there's a docker in my application pipelineKris Buytaert
 
OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert
OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert
OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert NETWAYS
 
Creating a mature puppet system
Creating a mature puppet systemCreating a mature puppet system
Creating a mature puppet systemrkhatibi
 
Creating a Mature Puppet System
Creating a Mature Puppet SystemCreating a Mature Puppet System
Creating a Mature Puppet SystemPuppet
 
Debugging Rails 3 Applications
Debugging Rails 3 ApplicationsDebugging Rails 3 Applications
Debugging Rails 3 ApplicationsNathan Broadbent
 
Gem That (2009)
Gem That (2009)Gem That (2009)
Gem That (2009)lazyatom
 
Automating MySQL operations with Puppet
Automating MySQL operations with PuppetAutomating MySQL operations with Puppet
Automating MySQL operations with PuppetKris Buytaert
 
Montreal.rb ruby debugging basics - march 20th 2012
Montreal.rb   ruby debugging basics - march 20th 2012Montreal.rb   ruby debugging basics - march 20th 2012
Montreal.rb ruby debugging basics - march 20th 2012Rafael Rosa
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLBarry Jones
 
Packaging is the Worst Way to Distribute Software, Except for Everything Else
Packaging is the Worst Way to Distribute Software, Except for Everything ElsePackaging is the Worst Way to Distribute Software, Except for Everything Else
Packaging is the Worst Way to Distribute Software, Except for Everything Elsemckern
 
Passing the Joel Test in the PHP World (phpbnl10)
Passing the Joel Test in the PHP World (phpbnl10)Passing the Joel Test in the PHP World (phpbnl10)
Passing the Joel Test in the PHP World (phpbnl10)Lorna Mitchell
 
Introduction to rails
Introduction to railsIntroduction to rails
Introduction to railsGo Asgard
 
Community and Github: 7/27/2011
Community and Github: 7/27/2011Community and Github: 7/27/2011
Community and Github: 7/27/2011Andy Lester
 
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12Puppet
 
Drupalhagen 2014 kiss omg ftw
Drupalhagen 2014   kiss omg ftwDrupalhagen 2014   kiss omg ftw
Drupalhagen 2014 kiss omg ftwArne Jørgensen
 
Wonderful world of (distributed) SCM or VCS
Wonderful world of (distributed) SCM or VCSWonderful world of (distributed) SCM or VCS
Wonderful world of (distributed) SCM or VCSVlatko Kosturjak
 

Semelhante a Writing a Ruby Gem for beginners (20)

Groovy there's a docker in my application pipeline
Groovy there's a docker in my application pipelineGroovy there's a docker in my application pipeline
Groovy there's a docker in my application pipeline
 
OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert
OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert
OSMC 2017 | Groovy There is a Docker in my Dashing Pipeline by Kris Buytaert
 
rubyonrails
rubyonrailsrubyonrails
rubyonrails
 
rubyonrails
rubyonrailsrubyonrails
rubyonrails
 
Creating a mature puppet system
Creating a mature puppet systemCreating a mature puppet system
Creating a mature puppet system
 
Creating a Mature Puppet System
Creating a Mature Puppet SystemCreating a Mature Puppet System
Creating a Mature Puppet System
 
Debugging Rails 3 Applications
Debugging Rails 3 ApplicationsDebugging Rails 3 Applications
Debugging Rails 3 Applications
 
Forensic Theming - DrupalCon London
Forensic Theming - DrupalCon LondonForensic Theming - DrupalCon London
Forensic Theming - DrupalCon London
 
Gem That (2009)
Gem That (2009)Gem That (2009)
Gem That (2009)
 
11 Ruby Gems
11 Ruby Gems11 Ruby Gems
11 Ruby Gems
 
Automating MySQL operations with Puppet
Automating MySQL operations with PuppetAutomating MySQL operations with Puppet
Automating MySQL operations with Puppet
 
Montreal.rb ruby debugging basics - march 20th 2012
Montreal.rb   ruby debugging basics - march 20th 2012Montreal.rb   ruby debugging basics - march 20th 2012
Montreal.rb ruby debugging basics - march 20th 2012
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
 
Packaging is the Worst Way to Distribute Software, Except for Everything Else
Packaging is the Worst Way to Distribute Software, Except for Everything ElsePackaging is the Worst Way to Distribute Software, Except for Everything Else
Packaging is the Worst Way to Distribute Software, Except for Everything Else
 
Passing the Joel Test in the PHP World (phpbnl10)
Passing the Joel Test in the PHP World (phpbnl10)Passing the Joel Test in the PHP World (phpbnl10)
Passing the Joel Test in the PHP World (phpbnl10)
 
Introduction to rails
Introduction to railsIntroduction to rails
Introduction to rails
 
Community and Github: 7/27/2011
Community and Github: 7/27/2011Community and Github: 7/27/2011
Community and Github: 7/27/2011
 
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
 
Drupalhagen 2014 kiss omg ftw
Drupalhagen 2014   kiss omg ftwDrupalhagen 2014   kiss omg ftw
Drupalhagen 2014 kiss omg ftw
 
Wonderful world of (distributed) SCM or VCS
Wonderful world of (distributed) SCM or VCSWonderful world of (distributed) SCM or VCS
Wonderful world of (distributed) SCM or VCS
 

Mais de ConFoo

Debugging applications with network security tools
Debugging applications with network security toolsDebugging applications with network security tools
Debugging applications with network security toolsConFoo
 
The business behind open source
The business behind open sourceThe business behind open source
The business behind open sourceConFoo
 
Security 202 - Are you sure your site is secure?
Security 202 - Are you sure your site is secure?Security 202 - Are you sure your site is secure?
Security 202 - Are you sure your site is secure?ConFoo
 
OWASP Enterprise Security API
OWASP Enterprise Security APIOWASP Enterprise Security API
OWASP Enterprise Security APIConFoo
 
Opensource Authentication and Authorization
Opensource Authentication and AuthorizationOpensource Authentication and Authorization
Opensource Authentication and AuthorizationConFoo
 
Introduction à la sécurité des WebServices
Introduction à la sécurité des WebServicesIntroduction à la sécurité des WebServices
Introduction à la sécurité des WebServicesConFoo
 
Le bon, la brute et le truand dans les nuages
Le bon, la brute et le truand dans les nuagesLe bon, la brute et le truand dans les nuages
Le bon, la brute et le truand dans les nuagesConFoo
 
The Solar Framework for PHP
The Solar Framework for PHPThe Solar Framework for PHP
The Solar Framework for PHPConFoo
 
Décrire un projet PHP dans des rapports
Décrire un projet PHP dans des rapportsDécrire un projet PHP dans des rapports
Décrire un projet PHP dans des rapportsConFoo
 
Server Administration in Python with Fabric, Cuisine and Watchdog
Server Administration in Python with Fabric, Cuisine and WatchdogServer Administration in Python with Fabric, Cuisine and Watchdog
Server Administration in Python with Fabric, Cuisine and WatchdogConFoo
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+ConFoo
 
Think Mobile First, Then Enhance
Think Mobile First, Then EnhanceThink Mobile First, Then Enhance
Think Mobile First, Then EnhanceConFoo
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in RubyConFoo
 
Scalable Architecture 101
Scalable Architecture 101Scalable Architecture 101
Scalable Architecture 101ConFoo
 
As-t-on encore besoin d'un framework web ?
As-t-on encore besoin d'un framework web ?As-t-on encore besoin d'un framework web ?
As-t-on encore besoin d'un framework web ?ConFoo
 
Pragmatic Guide to Git
Pragmatic Guide to GitPragmatic Guide to Git
Pragmatic Guide to GitConFoo
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.jsConFoo
 
An Overview of Flash Storage for Databases
An Overview of Flash Storage for DatabasesAn Overview of Flash Storage for Databases
An Overview of Flash Storage for DatabasesConFoo
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump StartConFoo
 
Develop mobile applications with Flex
Develop mobile applications with FlexDevelop mobile applications with Flex
Develop mobile applications with FlexConFoo
 

Mais de ConFoo (20)

Debugging applications with network security tools
Debugging applications with network security toolsDebugging applications with network security tools
Debugging applications with network security tools
 
The business behind open source
The business behind open sourceThe business behind open source
The business behind open source
 
Security 202 - Are you sure your site is secure?
Security 202 - Are you sure your site is secure?Security 202 - Are you sure your site is secure?
Security 202 - Are you sure your site is secure?
 
OWASP Enterprise Security API
OWASP Enterprise Security APIOWASP Enterprise Security API
OWASP Enterprise Security API
 
Opensource Authentication and Authorization
Opensource Authentication and AuthorizationOpensource Authentication and Authorization
Opensource Authentication and Authorization
 
Introduction à la sécurité des WebServices
Introduction à la sécurité des WebServicesIntroduction à la sécurité des WebServices
Introduction à la sécurité des WebServices
 
Le bon, la brute et le truand dans les nuages
Le bon, la brute et le truand dans les nuagesLe bon, la brute et le truand dans les nuages
Le bon, la brute et le truand dans les nuages
 
The Solar Framework for PHP
The Solar Framework for PHPThe Solar Framework for PHP
The Solar Framework for PHP
 
Décrire un projet PHP dans des rapports
Décrire un projet PHP dans des rapportsDécrire un projet PHP dans des rapports
Décrire un projet PHP dans des rapports
 
Server Administration in Python with Fabric, Cuisine and Watchdog
Server Administration in Python with Fabric, Cuisine and WatchdogServer Administration in Python with Fabric, Cuisine and Watchdog
Server Administration in Python with Fabric, Cuisine and Watchdog
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
 
Think Mobile First, Then Enhance
Think Mobile First, Then EnhanceThink Mobile First, Then Enhance
Think Mobile First, Then Enhance
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Scalable Architecture 101
Scalable Architecture 101Scalable Architecture 101
Scalable Architecture 101
 
As-t-on encore besoin d'un framework web ?
As-t-on encore besoin d'un framework web ?As-t-on encore besoin d'un framework web ?
As-t-on encore besoin d'un framework web ?
 
Pragmatic Guide to Git
Pragmatic Guide to GitPragmatic Guide to Git
Pragmatic Guide to Git
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
 
An Overview of Flash Storage for Databases
An Overview of Flash Storage for DatabasesAn Overview of Flash Storage for Databases
An Overview of Flash Storage for Databases
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump Start
 
Develop mobile applications with Flex
Develop mobile applications with FlexDevelop mobile applications with Flex
Develop mobile applications with Flex
 

Writing a Ruby Gem for beginners

  • 1. How to Write a Gem for Beginners Ken C oar
  • 2. Disclaimer ● Don't be confused by the session title; this isn't – How to write a 'gem for beginners' ● It's – 'How to write a gem' for beginners ● Also, Ken (that's me) doesn't profess to be an expert at any of this; he's just sharing experiences of what worked
  • 3. Why? ● You've seen all the Ruby gems available, and heard a lot about them. Where do they come from? How do you create one? And why would you want to? ● There is no One True Way to create a gem, and different people will give you different recommendations. ● This session describes one of the many paths available from 'why?' to 'ta-daah!' It worked for me; it might work for you too.
  • 4. Pick a Licence ● Why? Well, because Ruby is an interpreter, and offering up a gem to the chaos of the Internet is essentially opening the source code to the Universe ● Do you care how your code may be used? To save lives? To crack banks? To guide missiles? To run gigantic financial institutions? To run schools? Think about it. ● If you really don't care at all, maybe putting it in the public domain is the answer.
  • 5. Source Control ● You're gonna make mistakes, and want to back away from them – possibly hurriedly ● You might want to have co-developers ● Git is currently very popular with the Ruby crowd; after an initial bad reaction (because of a basic misunderstanding I had about it), I've come to prefer it myself ● There are also Subversion, CVS, Arch, BitKeeper, and who-knows-what other options ● You're going to want one, think about it up front
  • 6. Disorder of Battle ● These are not necessarily in order: – Set up source control – Set up the directory tree – Write tests – Learn Rake (if you haven't yet) – Document, document, document – Build – Upload
  • 7. Our victim: BitString ● I'm going to use one of my own gems as an example, pretending to create it anew.
  • 8. Creating the layout: newgem ● I've got a lot of projects up in the air; as a consequence, I like to prefix the directories for my gems with 'rubygem-' ● That doesn't play well with newgem, though, so we'll work around it later ● Here we go: – newgem bitstring cp erehwon/LICENCE.txt bitstring/
  • 9. What newgem did History.txt lib/ LICENCE.txt Manifest.txt PostInstall.txt Rakefile README.rdoc script/ test/
  • 10. Set up source control mv bitstring rubygem-bitstring cd rubygem-bitstring git init git add * echo '*~' >>.gitignore git add .gitignore git commit -m 'Initial commit'
  • 11. Write code! ● Now's the time to start actually writing the code. ● Or, if it's already written, copying it into the gem directory tree. ● Code goes under the 'lib/' subdirectory ● I've already got mine, so I just copy it in
  • 12. The lib/ tree $ tree lib lib ├── bitstring │   └── operators.rb └── bitstring.rb 1 directory, 2 files
  • 13. Now you've got your code. Does it work? ● Time to write tests. newgem started us off: $ tree test test ├── test_bitstring.rb └── test_helper.rb $ cat test/test_bitstring.rb require File.dirname(__FILE__) + '/test_helper.rb' class TestRubygemBitstring < Test::Unit::TestCase def setup end def test_truth assert true end end
  • 14. Documentation (rdoc) ● Okey, so your code works. You've tested it nigh unto death, and sent it off to the showers to recover. ● But wait! There's more! While it's in the shower, you need to bring in a tattoo artist to.. ● Okey, that analogy isn't going anywhere useful. Try this: – Add the internal rdoc documentation ● Build the docs with rake docs and look at them (on your local filesystem) in your browser
  • 15. The spec file ● The .gemspec file is what tells the gem build everything it needs to know ● Gemspecs can be complicated; fortunately, there's a shortcut to get you started: – rake gemspec ● After that, look at the foo.gemspec file and read up on gemspecs to see what changes you want/need to make
  • 16. Building your gem ● Once your code is written, your tests pass, your documentation is written (and verified), and your gemspec file has been created, you can actually build your gem! $ gem build bitstring.gemspec Successfully built RubyGem Name: bitstring Version: 1.0.0 File: bitstring-1.0.0.gem ● The name comes from the version and other details in the gemspec file
  • 17. Uploading ● Very simple! – gem push bitstring-1.0.0.gem ● Well, you need to have an account on the system first. Get one at – http://rubygems.org/sign_up
  • 18. Monitor on RubyGems ● Once your gem has been uploaded, you can see statistics about its usage at – https://rubygems.org/gems/yourgem
  • 19. Notes and hints ● Look at other gems ● So far, this has all been done in your local environment. Once you feel confident, consider creating a project for your gem at RubyForge and uploading your code there. – I highly recommend it!
  • 20. Wrapup ● Questions? Sure. Answers? Maybe. :-)