Это зависит от используемой вами реализации c Ruby.
В Рубиниус существует два определения. Первое определение является чрезвычайно базовым c, которое используется для реализации самого Рубиниуса. Он находится в core/alpha.rb
, самом первом файле, загруженном в ядро Rubinius:
class Module
def attr_accessor(name)
attr_reader(name)
attr_writer(name)
nil
end
end
Полнофункциональное определение находится в core/zed.rb
, самый последний файл, загруженный в ядро Рубиниуса:
class Module
def attr_accessor(*names)
vis = Rubinius::VariableScope.of_sender.method_visibility
names.each do |name|
Rubinius.add_reader name, self, vis
Rubinius.add_writer name, self, vis
end
return nil
end
private :attr_accessor
end
В Опал , он определен в opal/corelib/module.rb
:
class Module
def attr_accessor(*names)
attr_reader(*names)
attr_writer(*names)
end
end
В Iron Ruby определение находится в Src/Libraries/Builtins/ModuleOps.cs
:
// thread-safe:
[RubyMethod("attr_accessor", RubyMethodAttributes.PrivateInstance)]
public static void AttrAccessor(RubyScope/*!*/ scope, RubyModule/*!*/ self, [DefaultProtocol, NotNull]string/*!*/ name) {
DefineAccessor(scope, self, name, true, true);
}
// thread-safe:
[RubyMethod("attr_accessor", RubyMethodAttributes.PrivateInstance)]
public static void AttrAccessor(RubyScope/*!*/ scope, RubyModule/*!*/ self, [DefaultProtocol, NotNullItems]params string/*!*/[]/*!*/ names) {
foreach (string name in names) {
DefineAccessor(scope, self, name, true, true);
}
}
В J Ruby, оно определено в core/src/main/java/org/jruby/RubyModule.java
:
@JRubyMethod(name = "attr_accessor", rest = true, reads = VISIBILITY)
public IRubyObject attr_accessor(ThreadContext context, IRubyObject[] args) {
// Check the visibility of the previous frame, which will be the frame in which the class is being eval'ed
Visibility visibility = context.getCurrentVisibility();
for (int i = 0; i < args.length; i++) {
// This is almost always already interned, since it will be called with a symbol in most cases
// but when created from Java code, we might getService an argument that needs to be interned.
// addAccessor has as a precondition that the string MUST be interned
addAccessor(context, TypeConverter.checkID(args[i]), visibility, true, true);
}
return context.nil;
}