0:00
/
0:00
Transcript

Const_missing and AI: A Ruby Metaprogramming Experiment

Thinking about your code as part of your prompt

This week’s Works on My Machine demo video showcases another way you can use the code generation techniques I shared in AI Programs While I Sleep Using GitHub Actions. This time, instead of using a description of the code I want to generate, I show that if you use descriptive class names and variable names, that’s usually good enough for the AI to generate the code you want too - meaning you can design the interface you want and just have AI fill in the gaps.

What It Does

The multipass.rb module implements const_missing with the ability to generate implementations of the missing constants. It does this in a few ways:

  1. It determines what type of class it is - in the Sublayer framework Actions have a `call` method and Generators have a `generate` method

  2. It examines the code around where the missing constant was referenced to look at what it was called with, what the output is getting assigned to (if anything) and how it is being used later

  3. It sends that context to an LLM along with some examples of what an Action or a Generator looks like as few-shot examples to create the new implementation

  4. Once the code is created, it saves the class to a file in its respective folder to be edited or used later.

Why It Matters

Sure it doesn’t exactly work fully yet, but it hints at a few things I think are very interesting. First, that the more you think about your code as context in a prompt (whether you’re using it like this or within something like copilot), the more you can use the code you write as a meta-tool to guide an LLM to generate the code you want. Your class names and variable names are all part of a prompt, so the clearer and more descriptive they are, the more likely the LLM is going to generate the right thing.

Second, there’s a lot of code context we have access to at runtime with Ruby that when combined with an LLM’s code generation abilities can unlock a lot of new ideas that weren’t really possible before. What other programming paradigms or at least developer experiences are possible if we turn the magic up to 11 with LLMs?

And third, by combining multiple passes, few-shot examples for code generation, and architectures designed to let you slot in large amounts of AI generated code at once, you can start to get closer to a world where AI generated code “just works” in one-shot for all different kinds of use cases. Getting us much closer to the dream of just-in-time software.

How To Get It

All the code for this demo is available at sublayerapp/leeloo and the main example code is available in multipass.rb. There’s only one “pass” right now, but the plan is to expand this further and do a follow up video where we do multiple passes and can see the intermediate representation that the LLM generates to go from a step by step outline, to a script, to filling in the individual classes.

If this sparks any ideas, definitely reach out! Like I mentioned in the video, I’ve been thinking about Ruby metaprogramming and LLMs for a long time now so I’d love to chat with anyone who also finds this kind of stuff interesting!

Thanks for reading Works on My Machine! This post is public so feel free to share it.

Share