Slide 1

Slide 1 text

5 hidden gems in the ruby standard library sergio gil·iamserg.io·@porras

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

ruby is old

Slide 5

Slide 5 text

its standard library big

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Work

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

❤❤❤ ❤ ❤❤❤ ❤❤ ❤❤❤

Slide 12

Slide 12 text

❤ “Fame”

Slide 13

Slide 13 text

❤ “Fame” CSV JSON Logger Benchmark

Slide 14

Slide 14 text

❤ “Fame” CSV JSON Logger Benchmark REXML RSS RINDA

Slide 15

Slide 15 text

❤ “Fame” CSV JSON Logger Benchmark REXML RSS RINDA ¯\_(ツ)_/¯

Slide 16

Slide 16 text

❤ “Fame” CSV JSON Logger Benchmark REXML RSS RINDA ??? ¯\_(ツ)_/¯

Slide 17

Slide 17 text

❤ “Fame” CSV JSON Logger Benchmark REXML RSS RINDA ??? ¯\_(ツ)_/¯

Slide 18

Slide 18 text

net/http

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

buggy

Slide 25

Slide 25 text

buggy slow

Slide 26

Slide 26 text

buggy slow lack of features

Slide 27

Slide 27 text

buggy slow lack of features confusing api

Slide 28

Slide 28 text

no one ❤ it

Slide 29

Slide 29 text

no one it

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

set

Slide 34

Slide 34 text

{,,,}

Slide 35

Slide 35 text

it doesn’t keep order

Slide 36

Slide 36 text

it doesn’t keep order It doesn’t support duplicates

Slide 37

Slide 37 text

it doesn’t keep order It doesn’t support duplicates it’s very fast at finding elements

Slide 38

Slide 38 text

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!

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Array#include?

Slide 41

Slide 41 text

Array#include? Array#uniq

Slide 42

Slide 42 text

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')

Slide 43

Slide 43 text

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')

Slide 44

Slide 44 text

+--------+--------+ |items | win | +--------+--------+ |100 | x3 | |1000 | x60 | |10000 | x450 | |1000000 | x40000 | +--------+--------+

Slide 45

Slide 45 text

> (Set.new([1, 2, 3]) + Set.new([3, 4, 5])) - Set.new([1, 3, 5]) #=> #

Slide 46

Slide 46 text

Queue

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

require "thread"

Slide 49

Slide 49 text

list.each do |item| do_something(item) end

Slide 50

Slide 50 text

list.each do |item| do_something_slow(item) end

Slide 51

Slide 51 text

list.map do |item| Thread.new do do_something(item) end end.each(&:join)

Slide 52

Slide 52 text

huge_list.map do |item| Thread.new do do_something(item) end end.each(&:join)

Slide 53

Slide 53 text

huge_list.map do |item| Thread.new do do_something(item) end end.each(&:join) huge_list = File.open('huge.txt')

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

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)

Slide 56

Slide 56 text

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)

Slide 57

Slide 57 text

Rake

Slide 58

Slide 58 text

utilities for managing files and dependencies

Slide 59

Slide 59 text

utilities for managing files and dependencies part of the standard library since 1.9

Slide 60

Slide 60 text

Rake::FileList A DSL for file name patterns

Slide 61

Slide 61 text

files = Dir.glob("{lib,spec}/**/*.rb") files << "Rakefile" if File.exists?("Rakefile") files.reject! { |f| f =~ /example\.rb$/ }

Slide 62

Slide 62 text

require 'rake/file_list' files = Rake::FileList.new do |files| files.add("lib/**/*.rb") files.add("spec/**/*.rb") files.add("Rakefile") files.exclude(/example\.rb$/) end

Slide 63

Slide 63 text

Rake::FileTask clever timestamp based file dependencies

Slide 64

Slide 64 text

Rake::FileTask.define_task('checksum.md5' => files) do |t| system "cat #{files.join(' ')} | md5 > checksum.md5" end.invoke

Slide 65

Slide 65 text

$ touch "lib/hello world.rb"

Slide 66

Slide 66 text

$ touch "lib/hello world.rb" $ ruby md5.rb

Slide 67

Slide 67 text

$ 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

Slide 68

Slide 68 text

shellwords

Slide 69

Slide 69 text

shell command line parsing

Slide 70

Slide 70 text

require "shellwords" Shellwords.split('um dois três "trinta e um"') #=> ["um", "dois", "três", "trinta e um"]

Slide 71

Slide 71 text

filename = "Sergio's notes.txt" system "cat #{filename}"

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

filename = "Sergio's notes.txt" system "cat #{Shellwords.escape(filename)}"

Slide 74

Slide 74 text

Rake::FileTask.define_task('checksum.md5' => files) do |t| system "cat #{Shellwords.shelljoin(files)} | md5 > checksum.md5" end.invoke

Slide 75

Slide 75 text

FIDDLE

Slide 76

Slide 76 text

NATIVE EXTENSIONS

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

LESS GLUE CODE

Slide 79

Slide 79 text

LESS GLUE CODE THIS CODE IS RUBY

Slide 80

Slide 80 text

LESS GLUE CODE THIS CODE IS RUBY NO COMPILATION STEP

Slide 81

Slide 81 text

int add(int a, int b) { return a + b; }

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

require 'fiddle' require 'fiddle/import' module MyLib extend Fiddle::Importer dlload 'libmylib.so' extern 'int add(int, int)' end

Slide 84

Slide 84 text

require 'fiddle' require 'fiddle/import' module MyLib extend Fiddle::Importer dlload 'libmylib.so' extern 'int add(int, int)' end puts MyLib.add(11, 31) #=> 42

Slide 85

Slide 85 text

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) #=> #

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

PStore

Slide 88

Slide 88 text

NoSQL anyone?

Slide 89

Slide 89 text

NoSQL anyone? the 90’s

Slide 90

Slide 90 text

(Disclaimer: I might be doing other additional things for it to work) It Works!

Slide 91

Slide 91 text

Transactions

Slide 92

Slide 92 text

Transactions Fast lookup by key

Slide 93

Slide 93 text

Transactions Fast lookup by key Flexible schema

Slide 94

Slide 94 text

Transactions Fast lookup by key Flexible schema Pluggable backends

Slide 95

Slide 95 text

require 'pstore' store = PStore.new('mydata.data')

Slide 96

Slide 96 text

[] []= fetch

Slide 97

Slide 97 text

store['hello'] = 'world'

Slide 98

Slide 98 text

store['hello'] = 'world' # PStore::Error: not in transaction

Slide 99

Slide 99 text

store.transaction do store['hello'] = 'world' end

Slide 100

Slide 100 text

store.transaction do store['hello'] end #=> "world"

Slide 101

Slide 101 text

store.transaction do store['account1'] ||= 0 store['account2'] ||= 0 store['account1'] += 1 store['account2'] -= 1 end

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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}

Slide 107

Slide 107 text

require 'yaml/store' store = YAML::Store.new('data.yml') # rest of examples just work!

Slide 108

Slide 108 text

require 'yaml/store' store = YAML::Store.new('data.yml') # rest of examples just work! --- :data: :a: 11 :b: 2

Slide 109

Slide 109 text

require 'pstore' require 'json' class JSONStore < PStore def dump(table) JSON.dump(table) end def load(content) JSON.load(content) end end

Slide 110

Slide 110 text

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}

Slide 111

Slide 111 text

{"data":{"a":11,"b":2}}

Slide 112

Slide 112 text

Those are just some examples

Slide 113

Slide 113 text

Explore

Slide 114

Slide 114 text

Explore Be critical

Slide 115

Slide 115 text

Explore Be critical Don’t believe everything you hear

Slide 116

Slide 116 text

Explore Be critical Don’t believe everything you hear talk about your discoveries

Slide 117

Slide 117 text

MUito Obrigado rubyconf.pt sergio gil·iamserg.io·@porras

Slide 118

Slide 118 text

Links: http://www.mikeperham.com/2010/11/22/the-ruby-stdlib-is-a-ghetto/ http://architecturalatrocities.com/post/23659800703/the-ruby-standard-library-is-a-disgrace http://apocryph.org/archives/579 http://devblog.avdi.org/2015/09/04/poll-whats-your-favorite-ruby-http-client-library/ http://www.jstorimer.com/pages/ruby-core-classes-arent-thread-safe http://iamserg.io/writings/2014/queue-and-sized-queue/ http://blog.skylight.io/bending-the-curve-writing-safe-fast-native-gems-with-rust/ https://robm.me.uk/ruby/2014/01/25/pstore.html http://www.rubydoc.info/stdlib/set http://www.rubydoc.info/stdlib/thread/Queue http://www.rubydoc.info/stdlib/thread/SizedQueue http://www.rubydoc.info/gems/rake/Rake/FileList http://www.rubydoc.info/gems/rake/Rake/FileTask http://www.rubydoc.info/stdlib/shellwords http://www.rubydoc.info/stdlib/fiddle/Fiddle http://www.rubydoc.info/stdlib/pstore/PStore