Zeitwerk outside of rails

From wikinotes
Revision as of 01:25, 29 July 2023 by Will (talk | contribs) (Created page with "= In Gems = <blockquote> == initializing zeitwerk== <blockquote> <syntaxhighlight lang="ruby"> # my-project/lib/my_project.rb # load gems that may not load otherwise (ex.) require "sorbet-runtime" require "active_support/all" # load zeitwerk require "zeitwerk" loader = Zeitwerk::Loader.for_gem loader.enable_reloading loader.setup loader.eager_load # define your root module module MyProject end </syntaxhighlight> </blockquote><!-- initializing zeitwerk--> == console s...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In Gems

initializing zeitwerk

# my-project/lib/my_project.rb

# load gems that may not load otherwise (ex.)
require "sorbet-runtime"
require "active_support/all"

# load zeitwerk
require "zeitwerk"
loader = Zeitwerk::Loader.for_gem
loader.enable_reloading
loader.setup
loader.eager_load

# define your root module
module MyProject
end

console stubs

ruby irb, ruby pry and other consoles may not automatically (or properly) load zetiwerk.
you may want to create a stub

# my-project/bin/console
#!/usr/bin/env ruby

require "bundler/setup"
require "my_project"

require "irb"
IRB.start(__FILE__)