org.jruby.Ruby.newSymbol()方法的使用及代码示例

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

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

Ruby.newSymbol介绍

暂无

代码示例

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

private static IRubyObject fieldTypeToRuby(ThreadContext context, String typeName) {
  return context.runtime.newSymbol(typeName.replace("TYPE_", "").toLowerCase());
}

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

@JRubyMethod(name = "label=")
public IRubyObject setLabel(ThreadContext context, IRubyObject value) {
  String labelName = value.asJavaString();
  this.label = context.runtime.newSymbol(labelName.toLowerCase());
  this.builder.setLabel(
      DescriptorProtos.FieldDescriptorProto.Label.valueOf("LABEL_" + labelName.toUpperCase()));
  return context.runtime.getNil();
}

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

@JRubyMethod
public IRubyObject each(ThreadContext context, Block block) {
  Ruby runtime = context.runtime;
  for (Descriptors.EnumValueDescriptor enumValueDescriptor : descriptor.getValues()) {
    block.yield(context, runtime.newArray(runtime.newSymbol(enumValueDescriptor.getName()),
        runtime.newFixnum(enumValueDescriptor.getNumber())));
  }
  return runtime.getNil();
}

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

@JRubyMethod(meta = true)
public static IRubyObject lookup(ThreadContext context, IRubyObject recv, IRubyObject number) {
  RubyEnumDescriptor rubyEnumDescriptorescriptor = (RubyEnumDescriptor) getDescriptor(context, recv);
  Descriptors.EnumDescriptor descriptor = rubyEnumDescriptorescriptor.getDescriptor();
  Descriptors.EnumValueDescriptor value = descriptor.findValueByNumber(RubyNumeric.num2int(number));
  if (value == null) return context.runtime.getNil();
  return context.runtime.newSymbol(value.getName());
}

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

@JRubyMethod(name = "decode_json", meta = true)
public static IRubyObject decodeJson(ThreadContext context, IRubyObject recv, IRubyObject json) {
  Ruby runtime = context.runtime;
  RubyMessage ret = (RubyMessage) ((RubyClass) recv).newInstance(context, Block.NULL_BLOCK);
  RubyModule jsonModule = runtime.getClassFromPath("JSON");
  RubyHash opts = RubyHash.newHash(runtime);
  opts.fastASet(runtime.newSymbol("symbolize_names"), runtime.getTrue());
  IRubyObject[] args = new IRubyObject[] { Helpers.invoke(context, jsonModule, "parse", json, opts) };
  ret.initialize(context, args);
  return ret;
}

代码示例来源: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: bazelbuild/bazel

Descriptors.EnumValueDescriptor val =
    enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value));
if (val.getIndex() != -1) value = context.runtime.newSymbol(val.getName());

代码示例来源: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

msgdefAddField(context, "repeated", name, runtime.newSymbol("message"), number, mapentryDescName);
return runtime.getNil();

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

@JRubyMethod(name = {"to_h", "to_hash"})
public IRubyObject toHash(ThreadContext context) {
  Ruby runtime = context.runtime;
  RubyHash ret = RubyHash.newHash(runtime);
  for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) {
    IRubyObject value = getField(context, fdef);
    if (!value.isNil()) {
      if (fdef.isRepeated() && !fdef.isMapField()) {
        if (fdef.getType() != Descriptors.FieldDescriptor.Type.MESSAGE) {
          value = Helpers.invoke(context, value, "to_a");
        } else {
          RubyArray ary = value.convertToArray();
          for (int i = 0; i < ary.size(); i++) {
            IRubyObject submsg = Helpers.invoke(context, ary.eltInternal(i), "to_h");
            ary.eltInternalSet(i, submsg);
          }
          value = ary.to_ary();
        }
      } else if (value.respondsTo("to_h")) {
        value = Helpers.invoke(context, value, "to_h");
      } else if (value.respondsTo("to_a")) {
        value = Helpers.invoke(context, value, "to_a");
      }
    }
    ret.fastASet(runtime.newSymbol(fdef.getName()), value);
  }
  return ret;
}

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

private static void handleContentModelAnnotation(Class<? extends Processor> processor, RubyClass rubyClass) {
  Ruby rubyRuntime = rubyClass.getRuntime();
  if (processor.isAnnotationPresent(ContentModel.class)) {
    ContentModel contentModel = processor.getAnnotation(ContentModel.class);
    rubyClass.callMethod(rubyRuntime.getCurrentContext(), "option", new IRubyObject[]{
        rubyRuntime.newSymbol("content_model"),
        rubyRuntime.newSymbol(contentModel.value().substring(1))
    });
  }
}

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

private static void handleLocationAnnotation(Class<? extends Processor> processor, RubyClass rubyClass) {
  Ruby rubyRuntime = rubyClass.getRuntime();
  if (processor.isAnnotationPresent(Location.class)) {
    Location location = processor.getAnnotation(Location.class);
    rubyClass.callMethod(rubyRuntime.getCurrentContext(), "option", new IRubyObject[]{
        rubyRuntime.newSymbol("location"),
        rubyRuntime.newSymbol(location.value().optionValue().substring(1))
    });
  }
}

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

@Override
 public void register(IRubyObject registry) {
  registry.callMethod(rubyRuntime.getCurrentContext(), "block", new IRubyObject[]{rubyClass, rubyRuntime.newSymbol(blockName)});
 }
});

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

@Override
 public void register(IRubyObject registry) {
  registry.callMethod(rubyRuntime.getCurrentContext(), "block_macro", new IRubyObject[]{rubyClass, rubyRuntime.newSymbol(blockName)});
 }
});

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

@Override
 public void register(IRubyObject registry) {
  registry.callMethod(rubyRuntime.getCurrentContext(), "inline_macro", new IRubyObject[]{rubyClass, rubyRuntime.newSymbol(name)});
 }
});

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

@Override
 public void register(IRubyObject registry) {
  registry.callMethod(rubyRuntime.getCurrentContext(), "block", new IRubyObject[]{rubyClass, rubyRuntime.newSymbol(blockProcessor.getName())});
 }
});

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

@Override
 public void register(IRubyObject registry) {
  registry.callMethod(rubyRuntime.getCurrentContext(), "inline_macro", new IRubyObject[]{rubyClass, rubyRuntime.newSymbol(AbstractProcessorProxy.getName(inlineMacroProcessor))});
 }
});

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

@Override
 public void register(IRubyObject registry) {
  registry.callMethod(rubyRuntime.getCurrentContext(), "block_macro", new IRubyObject[]{rubyClass, rubyRuntime.newSymbol(AbstractProcessorProxy.getName(blockMacroProcessor))});
 }
});

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

@Override
 public void register(IRubyObject registry) {
  registry.callMethod(rubyRuntime.getCurrentContext(), "block", new IRubyObject[]{rubyRuntime.newString(blockProcessor), rubyRuntime.newSymbol(blockName)});
 }
});

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

private Cursor getSourceLocation(IRubyObject msg) {
  if (getRuntime().getHash().equals(msg.getType())) {
   final RubyHash hash = (RubyHash) msg;
   final Object sourceLocation = hash.get(getRuntime().newSymbol(LOG_PROPERTY_SOURCE_LOCATION));
   return new CursorImpl((IRubyObject) sourceLocation);
  }
  return null;
 }
}

相关文章

微信公众号

最新文章

更多

Ruby类方法