Fix attr reader/writer setting method.
@@ -224,31 +224,11 @@ func (c *RClass) setAttrWriter(args interface{}) {
case []Object:
for _, attr := range args {
attrName := attr.(*StringObject).Value
- 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
- }
- },
- }
-
- c.Methods.set(attrName+"=", m)
+ c.Methods.set(attrName+"=", generateAttrWriteMethod(attrName))
}
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
- }
- },
- }
-
- c.Methods.set(attrName+"=", m)
+ c.Methods.set(attrName+"=", generateAttrWriteMethod(attrName))
}
}
@@ -259,21 +239,30 @@ func (c *RClass) setAttrReader(args interface{}) {
case []Object:
for _, attr := range args {
attrName := attr.(*StringObject).Value
- m := &BuiltInMethodObject{
- Name: attrName,
+ c.Methods.set(attrName, generateAttrReadMethod(attrName))
+ }
+ case []string:
+ for _, attrName := range args {
+ c.Methods.set(attrName, generateAttrReadMethod(attrName))
+ }
+ }
+
+}
+
+func generateAttrWriteMethod(attrName string) *BuiltInMethodObject {
+ return &BuiltInMethodObject{
+ Name: attrName + "=",
Fn: func(receiver Object) builtinMethodBody {
return func(vm *VM, args []Object, blockFrame *callFrame) Object {
- v, _ := receiver.(*RObject).InstanceVariables.get("@" + attrName)
+ v := receiver.(*RObject).InstanceVariables.set("@"+attrName, args[0])
return v
}
},
}
-
- c.Methods.set(attrName, m)
}
- case []string:
- for _, attrName := range args {
- m := &BuiltInMethodObject{
+
+func generateAttrReadMethod(attrName string) *BuiltInMethodObject {
+ return &BuiltInMethodObject{
Name: attrName,
Fn: func(receiver Object) builtinMethodBody {
return func(vm *VM, args []Object, blockFrame *callFrame) Object {
@@ -282,11 +271,6 @@ func (c *RClass) setAttrReader(args interface{}) {
}
},
}
-
- c.Methods.set(attrName, m)
- }
- }
-
}
func (c *RClass) setAttrAccessor(args interface{}) {
