Skip to content

Instantly share code, notes, and snippets.

@usergenic
Created September 29, 2025 17:41
Show Gist options
  • Select an option

  • Save usergenic/dddfe7758de99fd190369452d483a9c5 to your computer and use it in GitHub Desktop.

Select an option

Save usergenic/dddfe7758de99fd190369452d483a9c5 to your computer and use it in GitHub Desktop.
example dsl to support prolog in ruby using instance eval
extend Prolog
facts do
# possibly symbols/strings are interchangeable for atoms
# since symbols are convenient
editor(:alice, :vim)
editor(:bob, :emacs)
editor(:carol, :vim)
editor(:devon, :emacs)
keyboard(:alice, :qwerty)
keyboard(:bob, :qwerty)
keyboard(:carol, :dvorak)
keyboard(:devon, :qwerty)
end
# Rules method makes all method calls handled by a method_missing handler on a RulesDefinition instance
rules do
can_pair(:@person_1, :@person_2) do
editor(:@person_1, :@x) &
editor(:@person_2, :@x) &
keyboard(:@person_1, :@y) &
keyboard(:@person_2, :@y)
end
end
query do
can_pair(:@x, :@y) do
# this block would be instance eval'd on a Prolog::Result
puts "#@x can pair with #@y"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment