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

Rummaging around the stdlib

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Rummaging around the stdlib

Presented at an ORUG meeting

Avatar for jswanner

jswanner

April 09, 2015

More Decks by jswanner

Other Decks in Programming

Transcript

  1. Definition of stdlib • Ships with the language • Separate

    from “core” • Opt-in parts of the language
  2. Common Parts • base64 • csv • erb • fileutils

    • json • logger • net/http • pathname • pp • securerandom • webrick • yaml
  3. Confusing Parts: Time $ irb >> Time.new(2015, 4, 9) =>

    2015-04-09 00:00:00 -0400 >> Time.parse('2015-04-09') NoMethodError: undefined method `parse' for Time:Class >> require 'time' => true >> Time.parse('2015-04-09') => 2015-04-09 00:00:00 -0400
  4. Confusing Parts: Date $ irb >> Date.new(2015, 4, 9) NameError:

    uninitialized constant Date >> require 'date' => true >> Date.new(2015, 4, 9) => #<Date: 2015-04-09 ((2457122j,0s,0n),+0s,2299161j)> >> Date.parse('2015-04-09') => #<Date: 2015-04-09 ((2457122j,0s,0n),+0s,2299161j)>
  5. Confusing Parts: RubyGems $ irb >> Gem => Gem >>

    require 'rails' => true $ ruby --disable-gems -S -- irb >> Gem NameError: uninitialized constant Gem >> require 'rails' LoadError: cannot load such file -- rails
  6. Lesser Known: ipaddr $ irb -r ipaddr >> IPAddr.new('10.0.0.256') IPAddr::InvalidAddressError:

    invalid address >> ip = IPAddr.new('10.0.0.10') => #<IPAddr: IPv4:10.0.0.10/255.255.255.255> >> ip.succ => #<IPAddr: IPv4:10.0.0.11/255.255.255.255> >> ip.ipv6? => false >> ip_range = ip.mask(24) => #<IPAddr: IPv4:10.0.0.0/255.255.255.0> >> ip_range.include?(ip) => true
  7. Lesser Known: irb #!/usr/bin/env ruby require 'irb' # require application

    code # perform application boot IRB.start(__FILE__)
  8. Buried Treasure: FileList $ irb -r rake/file_list >> existing =

    Rake::FileList.new("*.markdown") => ["a.markdown", "b.markdown", "c.markdown"] >> renamed = existing.ext(".md") => ["a.md", "b.md", "c.md"] >> existing.zip(renamed). ?> each { |from, to| FileUtils.mv(from, to) } => [["a.markdown", "a.md"], ["b.markdown", "b.md"], ["c.markdown", "c.md"]]
  9. FileList, cont’d $ tree ├── lib/ │ ├── a.rb │

    ├── b.rb │ └── c.rb └── test/ ├── a_test.rb └── c_test.rb
  10. FileList, cont’d $ irb -r rake/file_list >> source = Rake::FileList.new("lib/*.rb")

    => ["lib/a.rb", "lib/b.rb", "lib/c.rb"] >> tests = Rake::FileList.new("test/*_test.rb") => ["test/a_test.rb", "test/c_test.rb"] >> untested = source. ?> sub(‘lib/‘, 'test/').sub('.rb', '_test.rb'). ?> exclude { |f| tests.include?(f) } => ["test/b_test.rb"]
  11. YAML::Store $ irb -r yaml/store >> store = YAML::Store.new("store.yml") =>

    #<Psych::Store:0x007f9b8f5b2430 …> >> store.transaction do ?> store["people"] = ["dray", "jacob", "nate"] ?> store["time"] = Time.now ?> end => … >> store["people"] PStore::Error: not in transaction
  12. YAML::Store, cont’d $ cat store.yml --- people: - dray -

    jacob - nate time: 2015-04-09 16:23:58.394957000 -04:00
  13. Quirky: un, cont’d ruby -run -e cp -- [OPTION] SOURCE

    DEST ruby -run -e ln -- [OPTION] TARGET LINK_NAME ruby -run -e mv -- [OPTION] SOURCE DEST ruby -run -e rm -- [OPTION] FILE ruby -run -e mkdir -- [OPTION] DIR ruby -run -e rmdir -- [OPTION] DIR ruby -run -e install -- [OPTION] SOURCE DEST ruby -run -e touch -- [OPTION] FILE ruby -run -e wait_writable -- [OPTION] FILE ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION] ruby -run -e httpd -- [OPTION] DocumentRoot ruby -run -e help [COMMAND]
  14. Quirky: un, cont’d $ ruby -run -e httpd -- --port

    8888 . & […] INFO WEBrick::HTTPServer#start: port=8888 $ curl "http://localhost:8888/store.yml" --- people: - dray - jacob - nate time: 2015-04-09 16:23:58.394957000 -04:00
  15. Quirky: ubygems $ cat $RUBY_ROOT/lib/ruby/2.2.0/ubygems.rb # This file allows for

    the running of rubygems with a nice # command line look-and-feel: ruby -rubygems foo.rb #-- # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others. # All rights reserved. # See LICENSE.txt for permissions. #++ require 'rubygems'
  16. • CDC/ C. Goldsmith, P. Feorino, E. L. Palmer, W.

    R. McManus http:// en.wikipedia.org/wiki/HIV/AIDS#/media/File:HIV-budding-Color.jpg • http://commons.wikimedia.org/wiki/User:Nephron http://en.wikipedia.org/wiki/ Herpes_simplex_virus#/media/File:Herpes_simplex_virus_pap_test.jpg • http://en.wikipedia.org/wiki/Chlamydia_infection#/media/ File:Pap_smear_showing_clamydia_in_the_vacuoles_500x_H%26E.jpg