privates.cr

Ruby style of private methods definition for Crystal

privates

Ruby-style private blocks for Crystal.

Why

Crystal requires private def on every method. This shard adds a privates do ... end block so a group of definitions reads like Ruby's private section:

require "privates"

class Counter
  def value
    step + double
  end

  privates do
    def step
      1
    end

    def double
      step * 2
    end
  end
end

How it works

privates is a compile-time macro. It iterates the def expressions of the block and expands each one to private {{ exp }} — byte-for-byte the same AST the parser produces for a hand-written private def. No runtime code, no wrappers, no behavior changes: visibility is enforced by the compiler exactly as if you had written private def yourself. def self.foo works too.

Installation

dependencies:
  privates:
    path: ../myshards/privates

(or git/github once published)

Testing

crystal spec
Repository

privates.cr

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 3 hours ago
  • October 27, 2026
License

MIT License

Links
Synced at

Tue, 27 Oct 2026 12:14:34 GMT

Languages