Skip to content

Carton

Real boundaries for Ruby

Ruby apps draw boundaries with folders and namespaces. Ruby ignores them. Carton makes them real. Only a Carton's export crosses, enforced at runtime.

gem install carton

main.rb

require 'carton'

Billing = import_relative 'billing'
Billing.invoice(100) # => 110
Tax                  # NameError: billing's import

billing Carton

billing.rb

Tax = import_relative 'tax'

module Billing
  def self.invoice(total) = total + Tax.on(total)
end

export Billing

tax Carton

tax.rb

module Tax
  def self.on(total) = total / 10
end

export Tax

Ruby, I love you
but we need
boundaries

Open classes and global constants are great until the app gets big. Then a money helper in Billing changes what Shipping prints.

Linters miss what they can't parse. Services turn method calls into network calls. Carton draws the boundaries inside one process.

billing/core_ext.rb

class Integer
  def to_s = '$%.2f' % (self / 100.0)
end

shipping/label.rb

"Box #{3} of #{4}"
# => "Box $0.03 of $0.04"

shipping/ship.rb

Billing::Ledger.post(order) # internals

Gemfile.lock

faraday (1.10.6) # Billing wants 2.x

What the boundary holds

Only exports cross

Other Cartons get the export, not your constants, methods, or imports.

# billing.rb
TAX_RATE = 10
# main.rb
TAX_RATE  # NameError

Monkey patches stay put

ActiveSupport's String#blank? applies only in the Carton that loads it.

# billing.rb
''.blank?  # => true
# main.rb
''.blank?  # NoMethodError

Gem versions per Carton

Each Carton can bring its own bundle and gem versions.

# billing/Gemfile.lock
faraday (2.15.0)
# shipping/Gemfile.lock
faraday (1.10.6)

Draw your first boundary

Export a module from one file and import it from another. Carton is experimental and needs Ruby 4.0.7+ with RUBY_BOX=1.