Last active
September 17, 2025 07:54
-
-
Save denisdefreyne/333bd295a2b2937a065b9eee8ee3dd33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person = new Class {{ | |
firstName: :attr, | |
lastName: :attr, | |
fullName: function { | |
`${this.firstName} ${this.lastName}` | |
}, | |
greet: function(greeting: String) { | |
printLn(`${greeting}, ${this.fullName}!`) | |
}, | |
}} | |
person = Person.new(firstName: 'Denis', lastName: 'Defreyne') | |
person.greet(greeting: 'Hey') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Context | |
def initialize(this, kwargs1, kwargs2) | |
define_singleton_method(:this) { this } | |
kwargs1.zip(kwargs2).each do |kw1, kw2| | |
name = kw1[0] | |
value = kw2[1] | |
define_singleton_method name do | |
value | |
end | |
end | |
end | |
end | |
class Constructor | |
def function(**kwargs1, &block) | |
lambda do |this, **kwargs2| | |
ctx = Context.new(this, kwargs1, kwargs2) | |
ctx.instance_eval(&block) | |
end | |
end | |
end | |
def Class(&block) | |
defns = Constructor.new.instance_exec(&block) | |
Class.new do | |
define_method :initialize do |**kwargs| | |
kwargs.each { instance_variable_set '@' + _1.first.to_s, _1.last } | |
end | |
define_method :this do | |
self | |
end | |
defns.each do |name, defn| | |
case defn | |
when :attr | |
attr_accessor name | |
when Proc | |
define_method(name) do |**kwargs| | |
defn.call(self, **kwargs) | |
end | |
end | |
end | |
end | |
end | |
define_method('`') do |s| | |
s.gsub(/\$\{([^}]*)\}/) do | |
eval Regexp.last_match(1) | |
end | |
end | |
def printLn(thing) | |
puts(thing) | |
end | |
def new(thing) | |
thing | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hey, Denis Defreyne! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment