Namespace
Separating apps/libs into isolated spaces
• Load apps/libs in a space
• Hide changes from apps/libs in a namespace to other spaces
• Run methods de
fi
ned in a space with de
fi
nitions in the space
Slide 6
Slide 6 text
Before Namespace: Global Only
All classes/modules are shared in the entire Ruby process
Ruby Process
Application Code
App::Func
User
Library Code
DB::Client (v2)
Library Code
ActiveSupport (v7)
Slide 7
Slide 7 text
Collision: 2 versions of 1 library cause errors
Ruby Process
Library Code
DB::Client (v3)
😵 Application Code
App::Func
User
Library Code
DB::Client (v2)
Library Code
ActiveSupport (v7)
Before Namespace: Global Only
Slide 8
Slide 8 text
Ruby Process
Namespace
Load apps/libs in a space
Namespace
Application Code
App::Func
User
Library Code
DB::Client (v2)
Library Code
ActiveSupport (v7)
Slide 9
Slide 9 text
Ruby Process
Namespace
Hide changes from apps/libs in a namespace to other spaces
Namespace
Application Code
App::Func
User
Library Code
DB::Client (v2)
Library Code
ActiveSupport (v7)
Library Code
DB::Client (v3)
😀
Slide 10
Slide 10 text
Ruby Process
Namespace
Run methods de
fi
ned in a space with de
fi
nitions in the space
Namespace
Application Code
App::Func
User
Library Code
DB::Client (v2)
Library Code
ActiveSupport (v7)
Namespace
Library Code
DB::Client (v3)
Namespace
Application Code
App::Func2
User
Library Code
ActiveSupport (v6)
Application
Code
call call
call
Namespaceͷఆٛ
ྫ2: ࠐΈΫϥεఆٛͷมߋ
• class String ʹΑΔӨڹൣғͷݶఆ
• NamespaceͰͷΈ༗ޮʹ͍ͨ͠
• Namespaceͷ֎Ͱແޮʹ͍ͨ͠
• ಛఆͷൣғͰͷΈ༗ޮͳϝιου
→ Re
fi
nementsͰ?
# my_awesome.rb
class String
def blank?; false; end
end
class MyAwesome
def self.call; “yay”.blank?; end
end
# app.rb
ns = Namespace.new
ns.require(‘my_awesome’)
ns::MyAwesome.call() #=> false
“yay”.blank? # NoMethodError
Slide 23
Slide 23 text
Namespaceͷఆٛ
ྫ2: ࠐΈΫϥεఆٛͷมߋ: Implicit Re
fi
nements
# app.rb
ns = Namespace.new
ns.require(‘my_awesome’)
ns::MyAwesome.call()
“yay”.blank? # NoMethodError
# my_awesome.rb
class String
def blank?; false; end
end
class MyAwesome
def self.call = “yay”.blank?
end
# my_awesome.rb
using ns.refiner
class String
def blank?; false; end
end
class MyAwesome
def self.call = “yay”.blank?
end
• Namespace෦ʹϞδϡʔϧ(re
fi
ner)ΛͬͯImplicit
Re
fi
nementsʹ༻͍Δ
• NamespaceͰධՁ͞ΕΔ .rb ·ͣ
҉ʹusing͞ΕΔ
ಈ࡞͕ಁաతʹ
ॻ͖͑ΒΕΔ෦
Slide 24
Slide 24 text
Namespaceͷఆٛ
ྫ2: ࠐΈΫϥεఆٛͷมߋ: Implicit Re
fi
nements
# app.rb
ns = Namespace.new
ns.require(‘my_awesome’)
ns::MyAwesome.call()
“yay”.blank? # NoMethodError
rb
false; end
me
ll = “yay”.blank?
# my_awesome.rb
using ns.refiner
module ns.refiner
refine String do
def blank?; false; end
end
end
class MyAwesome
def self.call = “yay”.blank?
end
# my_awesome.rb
using ns.refiner
class String
def blank?; false; end
end
class MyAwesome
def self.call = “yay”.blank?
end
• class StringΛ
module re
fi
ner; re
fi
ne String ʹॻ͖͑ͯ
Re
fi
nementʹͳΔΑ͏ಡΈସ͑Δ ಈ࡞͕ಁաతʹ
ॻ͖͑ΒΕΔ෦
Slide 25
Slide 25 text
Namespaceͷఆٛ
ྫ2: ࠐΈΫϥεఆٛͷมߋ: Implicit Re
fi
nements
# app.rb
ns = Namespace.new
ns.require(‘my_awesome’)
ns::MyAwesome.call()
“yay”.blank? # NoMethodError
# my_awesome.rb
using ns.refiner
module ns.refiner
refine String do
def blank?; false; end
end
end
using ns.refiner
class MyAwesome
def self.call = “yay”.blank?
end
# my_awesome.rb
using ns.refiner
module ns.refiner
refine String do
def blank?; false; end
end
end
class MyAwesome
def self.call = “yay”.blank?
end
y_awesome.rb
ng ns.refiner
ss String
ef blank?; false; end
ss MyAwesome
ef self.call = “yay”.blank?
• ߋ৽͞ΕͨRe
fi
nementΛ༗ޮʹ͢ΔͨΊ
͏͍ͪͲusing͢Δ
ಈ࡞͕ಁաతʹ
ॻ͖͑ΒΕΔ෦
Slide 26
Slide 26 text
Namespaceͷ࣮
Implicit Re
fi
nementsʹΑΔ࣮ߦ࣌ఆٛߋ৽
• ϝιουՃ → Implicit Re
fi
nementsͰ࣮ݱ
• ఆՃ → re
fi
nerϞδϡʔϧʹఆΛՃͯ͠ࢀরՄೳʹ
• τοϓϨϕϧϝιουఆٛ → ObjectΛre
fi
ne͢Δ͜ͱͰରԠՄೳ