Make *RClass.setAttrReader() and .setAttrWriter() also accepts string slice
@@ -218,7 +218,10 @@ func (c *RClass) initializeInstance() *RObject {
return instance
}
-func (c *RClass) setAttrWriter(args []Object) {
+func (c *RClass) setAttrWriter(args interface{}) {
+
+ switch args := args.(type) {
+ case []Object:
for _, attr := range args {
attrName := attr.(*StringObject).Value
m := &BuiltInMethodObject{
@@ -233,9 +236,27 @@ func (c *RClass) setAttrWriter(args []Object) {
c.Methods.set(attrName+"=", m)
}
+ case []string:
+ for _, attrName := range args {
+ m := &BuiltInMethodObject{
+ Name: attrName + "=",
+ Fn: func(receiver Object) builtinMethodBody {
+ return func(vm *VM, args []Object, blockFrame *callFrame) Object {
+ v := receiver.(*RObject).InstanceVariables.set("@"+attrName, args[0])
+ return v
+ }
+ },
}
-func (c *RClass) setAttrReader(args []Object) {
+ c.Methods.set(attrName+"=", m)
+ }
+ }
+
+}
+
+func (c *RClass) setAttrReader(args interface{}) {
+ switch args := args.(type) {
+ case []Object:
for _, attr := range args {
attrName := attr.(*StringObject).Value
m := &BuiltInMethodObject{
@@ -250,6 +271,27 @@ func (c *RClass) setAttrReader(args []Object) {
c.Methods.set(attrName, m)
}
+ case []string:
+ for _, attrName := range args {
+ m := &BuiltInMethodObject{
+ Name: attrName,
+ Fn: func(receiver Object) builtinMethodBody {
+ return func(vm *VM, args []Object, blockFrame *callFrame) Object {
+ v, _ := receiver.(*RObject).InstanceVariables.get("@" + attrName)
+ return v
+ }
+ },
+ }
+
+ c.Methods.set(attrName, m)
+ }
+ }
+
+}
+
+func (c *RClass) setAttrAccessor(args interface{}) {
+ c.setAttrReader(args)
+ c.setAttrWriter(args)
}
var builtinGlobalMethods = []*BuiltInMethodObject{
@@ -357,9 +399,7 @@ var builtinClassClassMethods = []*BuiltInMethodObject{
Fn: func(receiver Object) builtinMethodBody {
return func(vm *VM, args []Object, blockFrame *callFrame) Object {
r := receiver.(*RClass)
-
- r.setAttrWriter(args)
- r.setAttrReader(args)
+ r.setAttrAccessor(args)
return r
}
