5 hidden gems of the Ruby Standard Library

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Sergio Gil
September 15, 2015

5 hidden gems of the Ruby Standard Library

Rubyconf Portugal 2015, Braga

Sergio Gil

September 15, 2015

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

118 slides
  1. set

  2. it doesn’t keep order It doesn’t support duplicates it’s very

    fast at finding elements doesn ’ t ma tter some times fast is nice, I like fast!
  3. BLACKLISTED_IPS = %w( 42.100.119.12 61.103.82.121 86.26.59.70 ... 114.28.117.113 79.45.68.85 )

    def blacklisted?(ip) BLACKLISTED_IPS.include?(ip) end blacklisted?('79.45.68.85')
  4. require 'set' BLACKLISTED_IPS = Set.new %w( 42.100.119.12 61.103.82.121 86.26.59.70 ...

    114.28.117.113 79.45.68.85 ) def blacklisted?(ip) BLACKLISTED_IPS.include?(ip) end blacklisted?('79.45.68.85')
  5. +--------+--------+ |items | win | +--------+--------+ |100 | x3 |

    |1000 | x60 | |10000 | x450 | |1000000 | x40000 | +--------+--------+
  6. num_workers = 20 queue = Queue.new threads = num_workers.times.map do

    Thread.new do until (item = queue.pop) == :END do_something(item) end end end list.each { |item| queue << item } num_workers.times { queue << :END } threads.each(&:join)
  7. num_workers = 20 queue = SizedQueue.new(num_workers * 2) threads =

    num_workers.times.map do Thread.new do until (item = queue.pop) == :END do_something(item) end end end list.each { |item| queue << item } num_workers.times { queue << :END } threads.each(&:join)
  8. $ touch "lib/hello world.rb" $ ruby md5.rb Calculating md5 sum

    cat: lib/hello: No such file or directory cat: world.rb: No such file or directory
  9. filename = "Sergio's notes.txt" system "cat #{filename}" sh: -c: line

    0: unexpected EOF while looking for matching `'' sh: -c: line 1: syntax error: unexpected end of file
  10. tracting libxml2-2.9.2.tar.gz into tmp/x86_64-apple-darwin14/ports/libxml2/2.9.2... OK nning patch with /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.6.2/ports/patches/libxml2/00 itialization-for-the-catalog-module.patch...

    nning 'patch' for libxml2 2.9.2... OK nning patch with /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.6.2/ports/patches/libxml2/00 E-2014-3660-fix.patch... nning 'patch' for libxml2 2.9.2... OK nning 'configure' for libxml2 2.9.2... OK nning 'compile' for libxml2 2.9.2... ERROR, review '/Library/Ruby/Gems/2.0.0/gems/nokogi 6_64-apple-darwin14/ports/libxml2/2.9.2/compile.log' to see what happened. * extconf.rb failed *** uld not create Makefile due to some reason, probably lack of necessary braries and/or headers. Check the mkmf.log file for more details. You may ed configuration options. ovided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby --help --clean --use-system-libraries --enable-static --disable-static --with-zlib-dir
  11. class MyNum module MyLib extend Fiddle::Importer dlload 'libmylib.so' extern 'int

    add(int, int)' end attr_reader :value def initialize(value) @value = value end def +(other) self.class.new(MyLib.add(value, other.value)) end protected :value end p MyNum.new(5) + MyNum.new(7) #=> #<MyNum:0x007f @value=12>
  12. class Whatever def initialize @pointer = MyLib.returns_a_pointer # this is

    a Fiddle::Pointer end def do_something(value) MyLib.uses_a_pointer(@pointer, value) end end
  13. store.transaction do store[:data] = { a: 1, b: 2} end

    store.transaction do store[:data][:a] += 10 end
  14. store.transaction do store[:data] = { a: 1, b: 2} end

    store.transaction do store[:data][:a] += 10 end store.transaction do store[:data] end
  15. store.transaction do store[:data] = { a: 1, b: 2} end

    store.transaction do store[:data][:a] += 10 end store.transaction do store[:data] end #=> {:a=>11, :b=>2}
  16. require 'pstore' require 'json' class JSONStore < PStore def dump(table)

    JSON.dump(table) end def load(content) JSON.load(content) end end
  17. store.transaction do store['data'] = { 'a' => 1, 'b' =>

    2 } end store.transaction do store['data']['a'] += 10 end store.transaction do store['data'] end #=> {"a"=>11, "b"=>2}

More Decks by Sergio Gil

Other Decks in Programming