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