本文整理汇总了Java中org.jruby.RubyString类的典型用法代码示例。如果您正苦于以下问题:Java RubyString类的具体用法?Java RubyString怎么用?Java RubyString使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RubyString类属于org.jruby包,在下文中一共展示了RubyString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: map2hash
import org.jruby.RubyString; //导入依赖的package包/类
@SuppressWarnings("unchecked") private static RubyHash map2hash(Ruby ruby, Map<String, Object> ourCaps) {
RubyHash hash = new RubyHash(ruby);
Set<String> keySet = ourCaps.keySet();
for (String key : keySet) {
RubySymbol keySym = RubySymbol.newSymbol(ruby, key);
Object v = ourCaps.get(key);
if (v instanceof String) {
hash.put(keySym, RubyString.newString(ruby, (String) v));
} else if(v instanceof Boolean) {
hash.put(keySym, RubyBoolean.newBoolean(ruby, (boolean) v));
} else if (v instanceof List) {
hash.put(keySym, map2list(ruby, (List<?>) v));
} else {
hash.put(keySym, map2hash(ruby, (Map<String, Object>) v));
}
}
return hash;
}
示例2: map2list
import org.jruby.RubyString; //导入依赖的package包/类
@SuppressWarnings("unchecked") private static RubyArray map2list(Ruby ruby, List<?> list) {
RubyArray array = new RubyArray(ruby, list.size());
int index = 0;
for (Object v : list) {
if (v instanceof String) {
array.set(index++, RubyString.newString(ruby, (String) v));
} else if(v instanceof Boolean) {
array.set(index++, RubyBoolean.newBoolean(ruby, (boolean) v));
} else if (v instanceof List) {
array.set(index++, map2list(ruby, (List<?>) v));
} else {
array.set(index++, map2hash(ruby, (Map<String, Object>) v));
}
}
return array;
}
示例3: hookIntoRuntime
import org.jruby.RubyString; //导入依赖的package包/类
/**
* Hooks this <code>TextAreaReadline</code> instance into the runtime,
* redefining the <code>Readline</code> module so that it uses this object.
* This method does not redefine the standard input-output streams. If you
* need that, use {@link #hookIntoRuntimeWithStreams(Ruby)}.
*
* @param runtime The runtime.
* @see #hookIntoRuntimeWithStreams(Ruby)
*/
public void hookIntoRuntime(final Ruby runtime) {
this.runtime = runtime;
/* Hack in to replace usual readline with this */
runtime.getLoadService().require("readline");
RubyModule readlineM = runtime.fastGetModule("Readline");
readlineM.defineModuleFunction("readline", new Callback() {
public IRubyObject execute(IRubyObject recv, IRubyObject[] args,
Block block) {
String line = readLine(args[0].toString());
if (line != null) {
return RubyString.newUnicodeString(runtime, line);
} else {
return runtime.getNil();
}
}
public Arity getArity() {
return Arity.twoArguments();
}
});
}
示例4: getValueFromView
import org.jruby.RubyString; //导入依赖的package包/类
public Object getValueFromView(Object view, String[] path) {
if(view instanceof IRubyObject) {
IRubyObject o = (IRubyObject)view;
for(String key : path) {
if(o == null || o.isNil()) { return null; }
o = o.callMethod(o.getRuntime().getCurrentContext(), "[]",
RubyString.newUnicodeString(o.getRuntime(), key));
}
if(o instanceof ConcreteJavaProxy) {
return ((ConcreteJavaProxy)o).getObject();
}
return ((o == null) || o.isNil()) ? null : o;
} else if(path.length == 0) {
return view; // to allow . value to work
}
return null;
}
示例5: valueIsTruthy
import org.jruby.RubyString; //导入依赖的package包/类
public boolean valueIsTruthy(Object value) {
if(value instanceof IRubyObject) {
IRubyObject o = (IRubyObject)value;
if(o == null) {
return false;
} else if(o instanceof RubyString) {
return !(((RubyString)o).isEmpty());
} else if(o instanceof RubyBoolean) {
return (o instanceof RubyBoolean.True);
} else if(o instanceof RubyArray) {
return !(((RubyArray)o).isEmpty());
} else if(value instanceof RubyNumeric) {
return ((RubyNumeric)value).getDoubleValue() != 0.0;
}
return false;
} else {
return super.valueIsTruthy(value);
}
}
示例6: applyLoadPaths
import org.jruby.RubyString; //导入依赖的package包/类
/**
* applyLoadPaths
*
* @param container
*/
protected void applyLoadPaths(Ruby runtime)
{
if (this._loadPaths != null && this._loadPaths.size() > 0)
{
IRubyObject object = runtime.getLoadService().getLoadPath();
if (object instanceof RubyArray)
{
RubyArray loadPathArray = (RubyArray) object;
// save copy for later
this._originalLoadPaths = (RubyArray) loadPathArray.dup();
// Add our custom load paths
for (String loadPath : this._loadPaths)
{
RubyString toAdd = runtime.newString(loadPath.replace('\\', '/'));
loadPathArray.append(toAdd);
}
}
}
}
示例7: fuzz
import org.jruby.RubyString; //导入依赖的package包/类
@JRubyMethod(name = "fuzz", required = 1, module = true)
public static IRubyObject fuzz(IRubyObject self, IRubyObject length) {
final Random rnd = new Random();
final long llen = length.convertToInteger().getLongValue();
final int len = (int) llen;
final byte[] bytes = new byte[len];
for (int i = 0; i < len; i++) {
switch (rnd.nextInt(4)) {
case 0:
bytes[i] = '\'';
break;
case 1:
bytes[i] = '\"';
break;
case 2:
bytes[i] = '\\';
break;
case 3:
bytes[i] = ' ';
break;
}
}
return RubyString.newString(self.getRuntime(), bytes);
}
示例8: getAttribute
import org.jruby.RubyString; //导入依赖的package包/类
/**
* Gets a value from an asciidoctor attributes map by key.
*
* @param attributes
* an asciidoctor attributes map
* @param name
* the key to look for
* @param defaultValue
* @param remove
* wether or not to remove the looked up attribute
* @return The attribute value or the given default value (if default value
* is null, the return value might also be null.)
*/
public static String getAttribute(Map<String, Object> attributes,
String name, String defaultValue, boolean remove) {
String type = defaultValue;
if (attributes.containsKey(name)) {
if (remove) {
Object removedObject = attributes.remove(name);
if (removedObject instanceof RubyString) {
type = ((RubyString) removedObject).decodeString();
} else if (removedObject instanceof String) {
type = (String) removedObject;
} else {
type = removedObject.toString();
}
} else {
Object retrievedObject = attributes.get(name);
if (!(retrievedObject instanceof String)) {
throw new RuntimeException(String.format(
"Did not expect %s in attributes map.",
retrievedObject.getClass().getName()));
}
type = (String) retrievedObject;
}
}
return type;
}
示例9: setName
import org.jruby.RubyString; //导入依赖的package包/类
/**
* This method allows the user to set the name of the alias of the FieldSchema of the encapsulated
* Schema. This method only works if the Schema has one FieldSchema.
*
* @param arg a RubyString to set the name to
* @return the new name
*/
@JRubyMethod(name = "name=")
public RubyString setName(IRubyObject arg) {
if (arg instanceof RubyString) {
if (internalSchema.size() != 1)
throw new RuntimeException("Can only set name if there is one schema present");
try {
internalSchema.getField(0).alias = arg.toString();
return (RubyString)arg;
} catch (FrontendException e) {
throw new RuntimeException("Unable to get field from Schema", e);
}
} else {
throw new RuntimeException("Improper argument passed to 'name=':" + arg);
}
}
示例10: rubyToPig
import org.jruby.RubyString; //导入依赖的package包/类
/**
* A type specific conversion routine.
*
* @param rbObject object to convert
* @return analogous Pig type
*/
@SuppressWarnings("unchecked")
public static Map<String, ?> rubyToPig(RubyHash rbObject) throws ExecException {
Map<String, Object> newMap = Maps.newHashMap();
for (Map.Entry<Object, Object> entry : (Set<Map.Entry<Object, Object>>)rbObject.entrySet()) {
Object key = entry.getKey();
if (!(key instanceof String || key instanceof RubyString || key instanceof RubySymbol))
throw new ExecException("Hash must have String or Symbol key. Was given: " + key.getClass().getName());
String keyStr = key.toString();
if (entry.getValue() instanceof IRubyObject) {
newMap.put(keyStr, rubyToPig((IRubyObject)entry.getValue()));
} else {
newMap.put(keyStr, entry.getValue());
}
}
return newMap;
}
示例11: should_have_gempath_in_ruby_env_when_created_with_gempath
import org.jruby.RubyString; //导入依赖的package包/类
@Test
public void should_have_gempath_in_ruby_env_when_created_with_gempath() {
// Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
final String gemPath = "/another/path";
assertThat(System.getenv("GEM_PATH"), notNullValue());
assertThat(System.getenv("GEM_HOME"), notNullValue());
// When: A new Asciidoctor instance is created passing in a null GEM_PATH
Asciidoctor asciidoctor = Asciidoctor.Factory.create(gemPath);
// Then: The org.jruby.JRuby instance does not see this variable
Ruby rubyRuntime = JRubyRuntimeContext.get();
RubyString rubyGemPath = rubyRuntime.newString(gemPath);
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is((Object) rubyGemPath));
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is((Object) rubyGemPath));
}
开发者ID:asciidoctor,项目名称:asciidoctorj,代码行数:17,代码来源:WhenAnAsciidoctorClassIsInstantiatedInAnEnvironmentWithGemPath.java
示例12: fromRubyToObject
import org.jruby.RubyString; //导入依赖的package包/类
private Object fromRubyToObject(IRubyObject o) {
Ruby runtime = this.handler.getRuntime();
if(o instanceof RubyString) {
return o.toJava(String.class);
} else if (o instanceof RubyFloat) {
return o.toJava(Double.class);
} else if (o instanceof RubyBoolean) {
return o.toJava(Boolean.class);
} else if (o instanceof XmlNodeSet) {
return (NodeList)o;
} else if (o instanceof RubyArray) {
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(runtime.getCurrentContext(), (RubyArray)o);
return (NodeList)xmlNodeSet;
} else /*if (o instanceof XmlNode)*/ {
return ((XmlNode) o).getNode();
}
}
示例13: createFromAttr
import org.jruby.RubyString; //导入依赖的package包/类
public static XmlNamespace createFromAttr(Ruby runtime, Attr attr) {
String prefixValue = getLocalNameForNamespace(attr.getName());
IRubyObject prefix_value;
if (prefixValue == null) {
prefix_value = runtime.getNil();
prefixValue = "";
} else {
prefix_value = RubyString.newString(runtime, prefixValue);
}
String hrefValue = attr.getValue();
IRubyObject href_value = RubyString.newString(runtime, hrefValue);
// check namespace cache
XmlDocument xmlDocument = (XmlDocument)getCachedNodeOrCreate(runtime, attr.getOwnerDocument());
xmlDocument.initializeNamespaceCacheIfNecessary();
XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefixValue, hrefValue);
if (xmlNamespace != null) return xmlNamespace;
// creating XmlNamespace instance
XmlNamespace namespace =
(XmlNamespace) NokogiriService.XML_NAMESPACE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
namespace.init(attr, prefix_value, href_value, prefixValue, hrefValue, xmlDocument);
// updateing namespace cache
xmlDocument.getNamespaceCache().put(namespace, attr.getOwnerElement());
return namespace;
}
示例14: run
import org.jruby.RubyString; //导入依赖的package包/类
@Override public String run(String script) {
try {
script = asciize(script);
IRubyObject evalScriptlet = interpreter.evalScriptlet(script);
if (evalScriptlet instanceof RubyString) {
return RubyScriptModel.inspect(evalScriptlet.toString());
} else {
return evalScriptlet.inspect().toString();
}
} catch (Throwable t) {
LOGGER.warning("Script:\n" + script);
t.printStackTrace();
}
return "";
}
示例15: valueToStringRepresentation
import org.jruby.RubyString; //导入依赖的package包/类
public String valueToStringRepresentation(Object value) {
if(value instanceof RubyArray || value instanceof RubyHash) {
return null;
}
if(value instanceof IRubyObject) {
IRubyObject o = (IRubyObject)value;;
if(o.isNil()) { return null; }
IRubyObject str = o.callMethod(o.getRuntime().getCurrentContext(), "to_s");
if(!(str instanceof RubyString)) { return null; }
return ((RubyString)str).decodeString();
}
return (value == null) ? null : value.toString();
}