From 585003668918f93a06c0bf2d95e1d03b98487e2d Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 30 Aug 2022 10:08:33 -0700 Subject: [PATCH] 12.7 --- ruby/lib/lox/lox_class.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ruby/lib/lox/lox_class.rb b/ruby/lib/lox/lox_class.rb index 44a9f37..017ea86 100644 --- a/ruby/lib/lox/lox_class.rb +++ b/ruby/lib/lox/lox_class.rb @@ -11,8 +11,21 @@ module Lox def find_method(name) = @methods[name] def to_s = name - def call(interpreter, args) = Instance.new(self) - def arity = 0 + + def call(interpreter, args) + instance = Instance.new(self) + + if init = find_method("init") + init.bind(instance).call(interpreter, args) + end + + instance + end + + def arity + init = find_method("init") + init ? init.arity : 0 + end end end