Skip to content

Instantly share code, notes, and snippets.

@randito
Created October 12, 2018 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randito/356e53b12ab01537054a75b21ed5c891 to your computer and use it in GitHub Desktop.
Save randito/356e53b12ab01537054a75b21ed5c891 to your computer and use it in GitHub Desktop.
Maditu POC (using blocks)
# other, self
listen { |other|
other_name = other.name
}
run { |other|
puts "Other name"
puts other.name
puts "Self name"
puts self.name
puts "Updating name"
self.name = update_name(other.name)
puts self.name
}
def update_name(name)
"The grand #{name}"
end
require 'ostruct'
class Code
attr_accessor :type, :block
def initialize(type, block)
@type = type
@block = block
end
end
module Codes
def listen(&block)
_codes << Code.new(:listen, block)
end
def claim(&block)
_codes << Code.new(:claim, block)
end
def run(&block)
_codes << Code.new(:run, block)
end
def _get_binding
you = self
Kernel.binding
end
def _clear_codes
@_codes = []
end
def _codes
@_codes ||= []
end
end
class Script
attr_accessor :filename
def initialize(filename)
@filename = filename
end
def source
File.read(filename)
end
def load_source(base)
base._clear_codes
b = base._get_binding
eval(source, b)
# base.instance_eval (source)
base
end
end
class Base < OpenStruct
include ::Codes
end
require './script'
obj1 = Base.new name: "object 1"
obj2 = Base.new name: "object 2"
script = Script.new 'hello1.maditu'
script.load_source(obj1)
# calling dynamic scripts against normal Ruby objects
#
# A POC of an engine that allows simple objects to react with
# one another using regular Ruby scripts
listen_1 = obj1._codes[0].block
run_1 = obj1._codes[1].block
listen_1.call(obj2)
run_1.call(obj2)
puts "Obj1"
puts obj1.inspect
puts "Obj2"
puts obj2.inspect
@randito
Copy link
Author

randito commented Oct 12, 2018

There's still no engine to make objects react with other. But it would take care of the obj1._codes[0].block.call(other_object) semantics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment