org.jruby.Ruby类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.6k)|赞(0)|评价(0)|浏览(161)

本文整理了Java中org.jruby.Ruby类的一些代码示例,展示了Ruby类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Ruby类的具体详情如下:
包路径:org.jruby.Ruby
类名称:Ruby

Ruby介绍

[英]The Ruby object represents the top-level of a JRuby "instance" in a given VM. JRuby supports spawning multiple instances in the same JVM. Generally, objects created under these instances are tied to a given runtime, for such details as identity and type, because multiple Ruby instances means there are multiple instances of each class. This means that in multi-runtime mode (or really, multi-VM mode, where each JRuby instance is a ruby "VM"), objects generally can't be transported across runtimes without marshaling. This class roots everything that makes the JRuby runtime function, and provides a number of utility methods for constructing global types and accessing global runtime structures.
[中]Ruby对象代表给定VM中JRuby“实例”的顶层。JRuby支持在同一个JVM中生成多个实例。通常,在这些实例下创建的对象与给定的运行时绑定,以获取标识和类型等详细信息,因为多个Ruby实例意味着每个类都有多个实例。这意味着,在多运行时模式(或者实际上是多VM模式,其中每个JRuby实例都是一个ruby“VM”)下,如果不进行封送处理,通常无法跨运行时传输对象。这个类是构成JRuby运行时功能的所有元素的根,并提供了许多用于构造全局类型和访问全局运行时结构的实用方法。

代码示例

代码示例来源:origin: bazelbuild/bazel

public static IRubyObject validateStringEncoding(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) {
  if (!(value instanceof RubyString))
    throw context.runtime.newTypeError("Invalid argument for string field.");
  switch(type) {
    case BYTES:
      value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::ASCII_8BIT"));
      break;
    case STRING:
      value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::UTF_8"));
      break;
    default:
      break;
  }
  value.setFrozen(true);
  return value;
}

代码示例来源:origin: bazelbuild/bazel

public static void createRubyDescriptorPool(Ruby runtime) {
  RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
  RubyClass cDescriptorPool = protobuf.defineClassUnder("DescriptorPool", runtime.getObject(), new ObjectAllocator() {
    @Override
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new RubyDescriptorPool(runtime, klazz);
    }
  });
  cDescriptorPool.defineAnnotatedMethods(RubyDescriptorPool.class);
  descriptorPool = (RubyDescriptorPool) cDescriptorPool.newInstance(runtime.getCurrentContext(), Block.NULL_BLOCK);
}

代码示例来源:origin: bazelbuild/bazel

switch (fieldType) {
  case INT32:
    return runtime.newFixnum((Integer) value);
  case INT64:
    return runtime.newFixnum((Long) value);
  case UINT32:
    return runtime.newFixnum(((Integer) value) & (-1l >>> 32));
  case UINT64:
    long ret = (Long) value;
    return ret >= 0 ? runtime.newFixnum(ret) :
        RubyBignum.newBignum(runtime, UINT64_COMPLEMENTARY.add(new BigInteger(ret + "")));
  case FLOAT:
    return runtime.newFloat((Float) value);
  case DOUBLE:
    return runtime.newFloat((Double) value);
  case BOOL:
    return (Boolean) value ? runtime.getTrue() : runtime.getFalse();
  case BYTES: {
    IRubyObject wrapped = runtime.newString(((ByteString) value).toStringUtf8());
    wrapped.setFrozen(true);
    return wrapped;
    IRubyObject wrapped = runtime.newString(value.toString());
    wrapped.setFrozen(true);
    return wrapped;
    return runtime.getNil();

代码示例来源:origin: bazelbuild/bazel

public static void checkNameAvailability(ThreadContext context, String name) {
  if (context.runtime.getObject().getConstantAt(name) != null)
    throw context.runtime.newNameError(name + " is already defined", name);
}

代码示例来源:origin: bazelbuild/bazel

public static void createRubyDescriptor(Ruby runtime) {
  RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
  RubyClass cDescriptor = protobuf.defineClassUnder("Descriptor", runtime.getObject(), new ObjectAllocator() {
    @Override
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new RubyDescriptor(runtime, klazz);
    }
  });
  cDescriptor.includeModule(runtime.getEnumerable());
  cDescriptor.defineAnnotatedMethods(RubyDescriptor.class);
}

代码示例来源:origin: bazelbuild/bazel

public static void createRubyFileDescriptor(Ruby runtime) {
  RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf");
  RubyClass cFieldDescriptor = mProtobuf.defineClassUnder("FieldDescriptor", runtime.getObject(), new ObjectAllocator() {
    @Override
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new RubyFieldDescriptor(runtime, klazz);
    }
  });
  cFieldDescriptor.defineAnnotatedMethods(RubyFieldDescriptor.class);
}

代码示例来源:origin: org.jruby/yecht

@JRubyMethod(required = 2, optional = 1, frame = true)
  public static IRubyObject scalar(IRubyObject self, IRubyObject[] args, Block block) {
    Ruby runtime = self.getRuntime();
    ThreadContext ctx = runtime.getCurrentContext();
    IRubyObject type_id = args[0];
    IRubyObject str = args[1];
    IRubyObject style = args.length == 2 ? runtime.getNil() : args[2];
    IRubyObject scalar = ((RubyModule)((RubyModule)runtime.getModule("YAML")).getConstant("Yecht")).getConstant("Scalar").callMethod(ctx, "new", new IRubyObject[]{type_id, str, style});
    outMark((IRubyObject)((RubyObject)self).fastGetInstanceVariable("@emitter"), scalar);
    return scalar;
  }
}

代码示例来源:origin: bazelbuild/bazel

private RubyModule buildClassFromDescriptor(ThreadContext context) {
  Ruby runtime = context.runtime;
  ObjectAllocator allocator = new ObjectAllocator() {
    @Override
    public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
      return new RubyMessage(runtime, klazz, descriptor);
    }
  };
  // rb_define_class_id
  RubyClass klass = RubyClass.newClass(runtime, runtime.getObject());
  klass.setAllocator(allocator);
  klass.makeMetaClass(runtime.getObject().getMetaClass());
  klass.inherit(runtime.getObject());
  RubyModule messageExts = runtime.getClassFromPath("Google::Protobuf::MessageExts");
  klass.include(new IRubyObject[] {messageExts});
  klass.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this);
  klass.defineAnnotatedMethods(RubyMessage.class);
  return klass;
}

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-jruby

@JRubyMethod(name = "initialize", visibility = PRIVATE, compat = RUBY2_0)
public IRubyObject initialize20(ThreadContext context, IRubyObject object, Block block) {
  Ruby runtime = context.runtime;
  RubySymbol each = runtime.newSymbol("each");
  
  // check for size
  if ((object.isNil() || runtime.getProc().isInstance(object)) ||
      runtime.getFloat().isInstance(object) && ((RubyFloat)object).getDoubleValue() == Float.POSITIVE_INFINITY) {
    // object is nil, a proc, or infinity; use it for size
    IRubyObject gen = context.runtime.getModule("JRuby").getClass("Generator").callMethod(context, "new", new IRubyObject[0], block);
    return initialize20(gen, each, NULL_ARRAY, object);
  }
  return initialize(object, each, NULL_ARRAY);
}

代码示例来源:origin: bazelbuild/bazel

IRubyObject valueType = args[2];
IRubyObject number = args[3];
IRubyObject typeClass = args.length > 4 ? args[4] : context.runtime.getNil();
    keyType.equals(RubySymbol.newSymbol(runtime, "enum")) ||
    keyType.equals(RubySymbol.newSymbol(runtime, "message")))
  throw runtime.newArgumentError("Cannot add a map field with a float, double, enum, or message type.");
RubyFieldDescriptor keyField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK);
keyField.setName(context, runtime.newString("key"));
keyField.setLabel(context, RubySymbol.newSymbol(runtime, "optional"));
keyField.setNumber(context, runtime.newFixnum(1));
keyField.setType(context, keyType);
mapentryDesc.addField(context, keyField);
RubyFieldDescriptor valueField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK);
valueField.setName(context, runtime.newString("value"));
valueField.setLabel(context, RubySymbol.newSymbol(runtime, "optional"));
valueField.setNumber(context, runtime.newFixnum(2));
valueField.setType(context, valueType);
if (! typeClass.isNil()) valueField.setSubmsgName(context, typeClass);
mapentryDesc.addField(context, valueField);
msgdefAddField(context, "repeated", name, runtime.newSymbol("message"), number, mapentryDescName);
return runtime.getNil();

代码示例来源:origin: asciidoctor/asciidoctorj

static ReaderImpl createReader(Ruby runtime, List<String> lines) {
  RubyArray rubyLines = runtime.newArray(lines.size());
  for (String line : lines) {
    rubyLines.add(runtime.newString(line));
  }
  RubyClass readerClass = runtime.getModule("Asciidoctor").getClass("Reader");
  return new ReaderImpl(readerClass.callMethod("new", rubyLines));
}

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-jruby

public static RubyModule createJavaInterfaceTemplateModule(ThreadContext context) {
  Ruby runtime = context.runtime;
  RubyModule javaInterfaceTemplate = runtime.defineModule("JavaInterfaceTemplate");
  RubyClass singleton = javaInterfaceTemplate.getSingletonClass();
  singleton.addReadAttribute(context, "java_class");
  singleton.defineAnnotatedMethods(JavaInterfaceTemplate.class);
  return javaInterfaceTemplate;
}

代码示例来源:origin: bazelbuild/bazel

@JRubyMethod(name = "method_missing", rest = true)
public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) {
  if (args.length == 1) {
    RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass);
    IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]);
    if (oneofDescriptor.isNil()) {
      if (!hasField(args[0])) {
        return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK);
      }
      return index(context, args[0]);
    }
    RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor;
    Descriptors.FieldDescriptor fieldDescriptor =
        oneofCases.get(rubyOneofDescriptor.getOneofDescriptor());
    if (fieldDescriptor == null)
      return context.runtime.getNil();
    return context.runtime.newSymbol(fieldDescriptor.getName());
  } else {
    // fieldName is RubySymbol
    RubyString field = args[0].asString();
    RubyString equalSign = context.runtime.newString(Utils.EQUAL_SIGN);
    if (field.end_with_p(context, equalSign).isTrue()) {
      field.chomp_bang(context, equalSign);
    }
    if (!hasField(field)) {
      return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK);
    }
    return indexSet(context, field, args[1]);
  }
}

代码示例来源:origin: bazelbuild/bazel

protected IRubyObject setField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor, IRubyObject value) {
  if (Utils.isMapEntry(fieldDescriptor)) {
    if (!(value instanceof RubyMap)) {
      throw context.runtime.newTypeError("Expected Map instance");
      addRepeatedField(fieldDescriptor, (RubyRepeatedField) value);
    } else {
      RubyArray ary = value.convertToArray();
      RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, ary);
      addRepeatedField(fieldDescriptor, repeatedField);
        fields.remove(oneofCase);
      if (value.isNil()) {
        oneofCases.remove(oneofDescriptor);
        fields.remove(fieldDescriptor);
      IRubyObject typeClass = context.runtime.getObject();
      boolean addValue = true;
      if (fieldType == Descriptors.FieldDescriptor.Type.MESSAGE) {
        typeClass = ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context);
        if (value.isNil()){
          addValue = false;
          Descriptors.EnumValueDescriptor val =
              enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value));
          if (val.getIndex() != -1) value = context.runtime.newSymbol(val.getName());
  return context.runtime.getNil();

代码示例来源:origin: bazelbuild/bazel

private RubyModule buildModuleFromDescriptor(ThreadContext context) {
  Ruby runtime = context.runtime;
  Utils.checkNameAvailability(context, name.asJavaString());
  RubyModule enumModule = RubyModule.newModule(runtime);
  for (Descriptors.EnumValueDescriptor value : descriptor.getValues()) {
    enumModule.defineConstant(value.getName(), runtime.newFixnum(value.getNumber()));
  }
  enumModule.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this);
  enumModule.defineAnnotatedMethods(RubyEnum.class);
  return enumModule;
}

代码示例来源:origin: org.jruby/jruby-complete

public static RubyString typeAsString(IRubyObject obj) {
  if (obj.isNil()) return obj.getRuntime().newString("nil");
  if (obj instanceof RubyBoolean) return obj.getRuntime().newString(obj.isTrue() ? "true" : "false");
  return obj.getMetaClass().getRealClass().rubyName();
}

代码示例来源:origin: bazelbuild/bazel

private IRubyObject wrapField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor, Object value) {
  if (value == null) {
    return context.runtime.getNil();
  }
  Ruby runtime = context.runtime;
  switch (fieldDescriptor.getType()) {
    case INT32:
    case INT64:
    case UINT32:
    case UINT64:
    case FLOAT:
    case DOUBLE:
    case BOOL:
    case BYTES:
    case STRING:
      return Utils.wrapPrimaryValue(context, fieldDescriptor.getType(), value);
    case MESSAGE:
      RubyClass typeClass = (RubyClass) ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context);
      RubyMessage msg = (RubyMessage) typeClass.newInstance(context, Block.NULL_BLOCK);
      return msg.buildFrom(context, (DynamicMessage) value);
    case ENUM:
      Descriptors.EnumValueDescriptor enumValueDescriptor = (Descriptors.EnumValueDescriptor) value;
      if (enumValueDescriptor.getIndex() == -1) { // UNKNOWN ENUM VALUE
        return runtime.newFixnum(enumValueDescriptor.getNumber());
      }
      return runtime.newSymbol(enumValueDescriptor.getName());
    default:
      return runtime.newString(value.toString());
  }
}

代码示例来源:origin: asciidoctor/asciidoctorj

@Override
public void register() {
 IRubyObject callback = extensionGroupClass.newInstance(rubyRuntime.getCurrentContext(), Block.NULL_BLOCK);
 asciidoctorModule.callMethod("register_extension_group",
   rubyRuntime.newString(this.groupName),
   callback,
   JavaEmbedUtils.javaToRuby(rubyRuntime, registrators));
}

代码示例来源:origin: org.jruby/jruby-complete

private RubyArray getLines(ParserConfiguration configuration, Ruby runtime, String file) {
  RubyArray list = null;
  IRubyObject scriptLines = runtime.getObject().getConstantAt("SCRIPT_LINES__");
  if (!configuration.isEvalParse() && scriptLines != null) {
    if (scriptLines instanceof RubyHash) {
      list = runtime.newArray();
      ((RubyHash) scriptLines).op_aset(runtime.getCurrentContext(), runtime.newString(file), list);
    }
  }
  return list;
}

代码示例来源:origin: org.jruby/jruby-core

/** rb_mod_extend_object
 *
 */
@JRubyMethod(name = "extend_object", required = 1, visibility = PRIVATE)
public IRubyObject extend_object(IRubyObject obj) {
  if (!isModule()) {
    throw getRuntime().newTypeError(this, getRuntime().getModule());
  }
  obj.getSingletonClass().includeModule(this);
  return obj;
}

相关文章

微信公众号

最新文章

更多

Ruby类方法