z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
r | reload slides |
n | toggle notes |
p | run preshow |
$ rails new the-truth
$ cd the-truth
$ vim config/database.yml
$ vim Gemfile
$ rails generate model journalist name
$ rails generate model article title body:text journalist:references
$ rake db:migrate
$ vim test/unit/{journalist,article}_test.rb
Martin Fowler: The Future is not just NoSQL, it’s Polyglot Persistence
class Address
# extend with your favourite persistence solution
end
class Address
# extend with your favourite persistence solution or:
attr_accessor :street, :postcode, :city, :country
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
class Address
# extend with your favourite persistence solution or:
attr_accessor :street, :postcode, :city, :country
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
# 9 Queen Street
# Edinburgh EH2 1JQ
# Scotland
class Address
# extend with your favourite persistence solution or:
attr_accessor :street, :postcode, :city, :country
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
class ScottishAddress
def formatted
[street, "#{city} #{postcode}", country].join "\n"
end
end
require 'delegate'
class Address
# extend with your favourite persistence solution or:
attr_accessor :street, :postcode, :city, :country
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
class ScottishAddress < SimpleDelegator
def formatted
[street, "#{city} #{postcode}", country].join "\n"
end
end
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
scottish_address = ScottishAddress.new address
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
scottish_address = ScottishAddress.new address
scottish_address.country # => Scotland
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
scottish_address = ScottishAddress.new address
scottish_address.country # => Scotland
puts scottish_address.formatted
# 9 Queen Street
# Edinburgh EH2 1JQ
# Scotland
class Address
# extend with your favourite persistence solution or:
attr_accessor :street, :postcode, :city, :country
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
class Address
# extend with your favourite persistence solution or:
attr_accessor :street, :postcode, :city, :country
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
class Person
# extend with your favourite persistence solution or:
attr_accessor :given_names, :surname, :title
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
class Address
# extend with your favourite persistence solution or:
attr_accessor :street, :postcode, :city, :country
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
class Person
# extend with your favourite persistence solution or:
attr_accessor :given_names, :surname, :title # *
def initialize args = {}
args.each { |key, value| send "#{key}=", value }
end
end
* please read Personal names around the world
class ScottishEnvelopeAddress
def initialize args
@person = args.fetch :person, Person.new
@address = args.fetch :address, Address.new
end
# Dr P. Rate
# 9 Queen Street
# Edinburgh EH2 1JQ
# Scotland
end
class ScottishEnvelopeAddress
def initialize args
@person = args.fetch :person, Person.new
@address = args.fetch :address, Address.new
end
def formatted
["#{title} #{given_names} #{surname}",
street, "#{city} #{postcode}", country].join "\n"
end
end
require 'forwardable'
class ScottishEnvelopeAddress
extend Forwardable
def initialize args
@person = args.fetch :person, Person.new
@address = args.fetch :address, Address.new
end
def_delegators :@person, :given_names, :surname, :title
def_delegators :@address, :street, :postcode, :city, :country
def formatted
["#{title} #{given_names} #{surname}",
street, "#{city} #{postcode}", country].join "\n"
end
end
person = Person.new given_names: 'P.', surname: 'Rate', title: 'Dr'
person = Person.new given_names: 'P.', surname: 'Rate', title: 'Dr'
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
person = Person.new given_names: 'P.', surname: 'Rate', title: 'Dr'
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
sea = ScottishEnvelopeAddress.new person: person, address: address
person = Person.new given_names: 'P.', surname: 'Rate', title: 'Dr'
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
sea = ScottishEnvelopeAddress.new person: person, address: address
sea.surname # => 'Rate'
sea.city # => 'Edinburgh'
person = Person.new given_names: 'P.', surname: 'Rate', title: 'Dr'
address = Address.new street: '9 Queen Street',
postcode: 'EH2 1JQ', city: 'Edinburgh', country: 'Scotland'
sea = ScottishEnvelopeAddress.new person: person, address: address
sea.surname # => 'Rate'
sea.city # => 'Edinburgh'
puts sea.formatted
# Dr P. Rate
# 9 Queen Street
# Edinburgh EH2 1JQ
# Scotland
class PrimeSelector
end
class PrimeSelector
def initialize numbers
@numbers = numbers
end
end
class PrimeSelector
def initialize numbers
@numbers = numbers
end
def primes
@numbers.select { |number| prime? number }
end
end
class PrimeSelector
def initialize numbers
@numbers = numbers
end
def primes
@numbers.select { |number| prime? number }
end
private
def prime? number
(2..Math.sqrt(number).floor).all? { |i| (number % i).nonzero? }
end
end
require 'logger'
require 'rcapture'
class Logging
end
require 'logger'
require 'rcapture'
class Logging
def initialize log
@log = Logger.new log
add_logging
end
end
require 'logger'
require 'rcapture'
class Logging
def initialize log
@log = Logger.new log
add_logging
end
private
def add_logging
PrimeSelector.class_eval { include RCapture::Interceptable }
end
end
require 'logger'
require 'rcapture'
class Logging
def initialize log
@log = Logger.new log
add_logging
end
private
def add_logging
PrimeSelector.class_eval { include RCapture::Interceptable }
PrimeSelector.capture_pre methods: :prime? do |point|
@log.debug "prime? #{point.args.first}"
end
end
end
require 'logger'
require 'rcapture'
class Logging
def initialize log
@log = Logger.new log
add_logging
end
private
def add_logging
PrimeSelector.class_eval { include RCapture::Interceptable }
PrimeSelector.capture_pre methods: :prime? do |point|
@log.debug "prime? #{point.args.first}"
end
PrimeSelector.capture_post methods: :prime? do |point|
@log.debug "prime? → #{point.return}"
end
end
end
Logging.new $stdout
PrimeSelector.new(2..10).primes
Logging.new $stdout
PrimeSelector.new(2..10).primes
# D, [2012-06-29T00:25:21.746960 #31892] DEBUG -- : prime? 2
# D, [2012-06-29T00:25:21.747180 #31892] DEBUG -- : prime? → true
# D, [2012-06-29T00:25:21.747265 #31892] DEBUG -- : prime? 3
# D, [2012-06-29T00:25:21.747347 #31892] DEBUG -- : prime? → true
# D, [2012-06-29T00:25:21.747460 #31892] DEBUG -- : prime? 4
# D, [2012-06-29T00:25:21.747550 #31892] DEBUG -- : prime? → false
# D, [2012-06-29T00:25:21.747629 #31892] DEBUG -- : prime? 5
# D, [2012-06-29T00:25:21.747740 #31892] DEBUG -- : prime? → true
# D, [2012-06-29T00:25:21.747818 #31892] DEBUG -- : prime? 6
# D, [2012-06-29T00:25:21.747900 #31892] DEBUG -- : prime? → false
# D, [2012-06-29T00:25:21.747977 #31892] DEBUG -- : prime? 7
# D, [2012-06-29T00:25:21.748072 #31892] DEBUG -- : prime? → true
# D, [2012-06-29T00:25:21.748149 #31892] DEBUG -- : prime? 8
# D, [2012-06-29T00:25:21.748232 #31892] DEBUG -- : prime? → false
# D, [2012-06-29T00:25:21.748309 #31892] DEBUG -- : prime? 9
# D, [2012-06-29T00:25:21.748406 #31892] DEBUG -- : prime? → false
# D, [2012-06-29T00:25:21.748482 #31892] DEBUG -- : prime? 10
# D, [2012-06-29T00:25:21.748563 #31892] DEBUG -- : prime? → false
module Data
class Person
# …
end
class Address
# …
end
end
module Roles
module Landlord
def rent_a_place address, person
# rent only if the person is credible (as a tenant)
end
end
module Tenant
def credible?
# …
end
end
end
module Contexts
class RentingAPlace
def initialize address, person_a, person_b
@address = address
@landlord = person_a.extend Roles::Landlord
@tenant = person_b.extend Roles::Tenant
end
end
def call
# …
@landlord.rent_a_place @address, @tenant
# …
end
end
address = … # probably fetch from persistence store
person_a = … # probably fetch from persistence store
person_b = … # probably fetch from persistence store
renting = Contexts::RentingAPlace.new address, person_a, person_b
renting.call
require 'candy'
require 'candy'
class Conference
include Candy::Piece
end
require 'candy'
class Conference
include Candy::Piece
end
src = Conference.new
# connects to localhost:27017 and ‘chastell’ db if needed
# and saves a new document to the ‘Conference’ collection
require 'candy'
class Conference
include Candy::Piece
end
src = Conference.new
# connects to localhost:27017 and ‘chastell’ db if needed
# and saves a new document to the ‘Conference’ collection
src.location = 'Edinburgh' # method_missing resaves
require 'candy'
class Conference
include Candy::Piece
end
src = Conference.new
# connects to localhost:27017 and ‘chastell’ db if needed
# and saves a new document to the ‘Conference’ collection
src.location = 'Edinburgh' # method_missing resaves
src.events = { parties: { saturday: 'Zoë Keating concert' } }
src.events.parties.saturday #=> 'Zoë Keating concert'
rails g model
-drive you design
class Integer
def simple_prime?
(2..Math.sqrt(self).floor).all? { |i| (self % i).nonzero? }
end
end
class Integer
def simple_prime?
(2..Math.sqrt(self).floor).all? { |i| (self % i).nonzero? }
end
def clever_prime?
return true if self == 2
return false if self.even?
3.step(Math.sqrt(self).floor, 2).all? { |i| (self % i).nonzero? }
end
end
require 'prime'
class Integer
def simple_prime?
(2..Math.sqrt(self).floor).all? { |i| (self % i).nonzero? }
end
def clever_prime?
return true if self == 2
return false if self.even?
3.step(Math.sqrt(self).floor, 2).all? { |i| (self % i).nonzero? }
end
end
require 'benchmark'
require 'prime'
class Integer
def simple_prime?
(2..Math.sqrt(self).floor).all? { |i| (self % i).nonzero? }
end
def clever_prime?
return true if self == 2
return false if self.even?
3.step(Math.sqrt(self).floor, 2).all? { |i| (self % i).nonzero? }
end
end
range = 2..1_000_000
Benchmark.bmbm do |bench|
bench.report('simple:') { range.each &:simple_prime? }
bench.report('clever:') { range.each &:clever_prime? }
bench.report('stdlib:') { range.each &:prime? }
end
$ ruby prime_bench.rb
Rehearsal -------------------------------------------
simple: 24.690000 0.000000 24.690000 ( 24.709989)
clever: 13.120000 0.000000 13.120000 ( 13.130319)
stdlib: 33.480000 0.000000 33.480000 ( 33.500611)
--------------------------------- total: 71.290000sec
user system total real
simple: 24.780000 0.000000 24.780000 ( 24.804631)
clever: 13.180000 0.000000 13.180000 ( 13.194177)
stdlib: 33.120000 0.000000 33.120000 ( 33.158172)