Sloprb
Stop writing code, let LLMs slop it for you at boot time 🦾.
SlopRB uses Require Hooks to intercept Ruby's require calls and generate some code for you using LLMs. Currently supported scenarios:
- Slop method implementations on the fly. Just mark a method with
# :slop:, describe what it should do in comments, and SlopRB fills in the body when the file is loaded. - Auto-correct syntax errors. When loading a file fails with a SyntaxError, the LLM fixes it, persists the fix, and retries the load.
Installation
Add to your Gemfile:
gem "sloprb"
Slopping methods
- You write a method stub with a
# :slop:marker and optional doc comments describing the desired behavior. - When the file is required, SlopRB parses it with Prism, extracts all slop-marked methods, and sends the source to an LLM.
- The LLM generates the method bodies, SlopRB splices them into the source, validates syntax, and returns the transformed code to Ruby.
Consider an example:
class Calculator
# Evaluates the passed arithmetic expression.
def evaluate(expr) # :slop:
end
end
puts Calculator.new.evaluate("2 + 2 * 3") #=> 8
You can also provide a sketch of the method or some hints in the body:
class Greeting
# Generate a friendly greeting message for the given name.
def greet(name) # :slop:
stable_index = ... # generate stable index based on name
samples = [...] # three random greeting phrases (avoid common, most popular options, be creative)
# return <greeting>, <name> in the end
end
end
puts Greeting.new.greet("Vova")
Setup
Load sloprb/setup before any code containing slop-marked methods:
require "sloprb/setup"
This configures the LLM provider (auto-detected from environment variables) and registers the Require Hooks source transformer.
Using with ruby executable
You can also setup SlopRB via a command-line switch (which is just a #require statement):
ruby -rbslop examples/calculator.rb
Use -rbslop/debug to also write the generated source to <path>.slop.rb files:
ruby -rbslop/debug examples/greeting.rb
Marking methods
Add # :slop: as a trailing comment on any def line:
class Stoplight
COLORS = %i[green yellow red].freeze
# Initialize the stoplight with a starting color.
# Default to green.
def initialize(color = :green) # :slop:
end
# Advance the stoplight to the next color in the cycle:
# green -> yellow -> red -> green
def next # :slop:
end
end
Comments immediately above the def line are sent to the LLM as guidance. The enclosing class/module context, method parameters, and surrounding source are also included in the prompt.
Caching with Bootsnap
If you use Bootsnap, the generated code is automatically cached and invalidated on every source file change thanks to the Bootsnap support in Require Hooks. You can try it yourself using our examples:
$ OPENAI_API_KEY=<your key> bundle exec ruby -Ilib -r ./examples/bootsnap -rbslop examples/calculator.rb "42 / 7"
6.0
# now, you can run it without LLM creds, and it will still work
bundle exec ruby -Ilib -r ./examples/bootsnap -rbslop examples/calculator.rb "25 + 26"
51.0
Eating typos
SlopRB's Typoeater extension automatically fixes syntax errors. When a file fails to load with a SyntaxError, the LLM attempts to fix it, validates the correction with Prism, and retries the load — transparently.
require "sloprb/typoeater"
Or via command line:
ruby -r sloprb/typoeater my_script.rb
When a required file has a syntax error, Typoeater:
- Captures the error and reads the file source
- Sends both to the LLM for correction
- Validates the fix with Prism (bad LLM output = original error raised, file untouched)
- Overwrites the file and retries the load (once)
Typoeater works independently from slop method generation — you can use either or both. When both are active, they share the same LLM configuration.
LLM configuration
SlopRB auto-detects the LLM provider from environment variables, checked in this order:
| Provider | Required env var | Optional env vars |
|---|---|---|
| OpenAI | OPENAI_API_KEY |
SLOPRB_MODEL |
| Anthropic | ANTHROPIC_API_KEY |
SLOPRB_MODEL |
| Ollama | OLLAMA_BASE_URL, SLOPRB_MODEL |
For OpenAI and Anthropic, SLOPRB_MODEL is optional (sensible defaults are used). For Ollama, it is required.
Set SLOPRB_MODEL to override the default model:
SLOPRB_MODEL=claude-sonnet-4-6 ruby -r bslop examples/calculator.rb
Enable debug output with SLOPRB_DEBUG=1 to write .slop.rb files with the generated source.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/sloprb.
License
The gem is available as open source under the terms of the MIT License.