本文整理汇总了Java中org.jruby.RubyHash类的典型用法代码示例。如果您正苦于以下问题:Java RubyHash类的具体用法?Java RubyHash怎么用?Java RubyHash使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RubyHash类属于org.jruby包,在下文中一共展示了RubyHash类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: map2hash
import org.jruby.RubyHash; //导入依赖的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: unapplyEnvironment
import org.jruby.RubyHash; //导入依赖的package包/类
/**
* unapplyEnvironment
*/
protected void unapplyEnvironment()
{
Ruby runtime = this.getRuntime();
IRubyObject env = runtime.getObject().getConstant(ENV_PROPERTY);
if (env != null && env instanceof RubyHash)
{
RubyHash hash = (RubyHash) env;
// restore original content
hash.replace(runtime.getCurrentContext(), this._originalEnvironment);
// lose reference
this._originalEnvironment = null;
}
}
示例3: rubyToPig
import org.jruby.RubyHash; //导入依赖的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;
}
示例4: pigToRuby
import org.jruby.RubyHash; //导入依赖的package包/类
/**
* A type specific conversion routine for Pig Maps. This only deals with maps
* with String keys, which is all that Pig supports.
*
* @param ruby the Ruby runtime to create objects in
* @param object map to convert. In Pig, only maps with String keys are
* supported
* @return analogous Ruby type
* @throws ExecException object contains a key that can't be convert to a Ruby type
*/
public static <T> RubyHash pigToRuby(Ruby ruby, Map<T, ?> object) throws ExecException {
RubyHash newMap = RubyHash.newHash(ruby);
boolean checkType = false;
for (Map.Entry<T, ?> entry : object.entrySet()) {
T key = entry.getKey();
if (!checkType) {
if (!(key instanceof String))
throw new ExecException("pigToRuby only supports converting Maps with String keys");
checkType = true;
}
newMap.put(key, pigToRuby(ruby, entry.getValue()));
}
return newMap;
}
示例5: createInline
import org.jruby.RubyHash; //导入依赖的package包/类
public Inline createInline(AbstractBlock parent, String context, String text, Map<String, Object> attributes, Map<String, Object> options) {
options.put(Options.ATTRIBUTES, attributes);
IRubyObject rubyClass = rubyRuntime.evalScriptlet("Asciidoctor::Inline");
RubyHash convertedOptions = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);
// FIXME hack to ensure we have the underlying Ruby instance
try {
parent = parent.delegate();
} catch (Exception e) {}
Object[] parameters = {
parent,
RubyUtils.toSymbol(rubyRuntime, context),
text,
convertedOptions };
return (Inline) JavaEmbedUtils.invokeMethod(rubyRuntime, rubyClass,
"new", parameters, Inline.class);
}
示例6: createBlock
import org.jruby.RubyHash; //导入依赖的package包/类
private Block createBlock(AbstractBlock parent, String context,
Map<Object, Object> options) {
IRubyObject rubyClass = rubyRuntime.evalScriptlet("Asciidoctor::Block");
RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(rubyRuntime,
options);
// FIXME hack to ensure we have the underlying Ruby instance
try {
parent = parent.delegate();
} catch (Exception e) {}
Object[] parameters = {
parent,
RubyUtils.toSymbol(rubyRuntime, context),
convertMapToRubyHashWithSymbols };
return (Block) JavaEmbedUtils.invokeMethod(rubyRuntime, rubyClass,
"new", parameters, Block.class);
}
示例7: testWordCount
import org.jruby.RubyHash; //导入依赖的package包/类
@Test
public void testWordCount() throws IOException {
logger.debug("Testing word count.");
String rubyCode = MemnonMapReduce.readFile("src/test/resources/wordcount.rb", Charset.defaultCharset());
RubyRunner rubyRunner = new RubyRunner(rubyCode, new ArrayList<URL>());
Row mockRow = new MockRow(new String[] { "title", "content" }, new String[] { "buff",
"buffalo in buffalo buffalo buffalo buffalo in cleveland" });
Object mapOutput = rubyRunner.runMap(mockRow);
assertValidResult(mapOutput);
List<String> keys = ResultHelper.getKeys(mapOutput);
List<String> values = ResultHelper.getValues(mapOutput);
assertEquals(8, keys.size());
assertEquals("buffalo", keys.get(0));
assertEquals(8, values.size());
assertEquals("1", values.get(0));
List<Text> reduceValues = new ArrayList<Text>();
reduceValues.add(new Text("1"));
reduceValues.add(new Text("1"));
reduceValues.add(new Text("1"));
reduceValues.add(new Text("1"));
RubyHash reduceOutput = (RubyHash) rubyRunner.runReduce("buffalo", reduceValues);
assertNotNull(reduceOutput);
assertEquals("4", reduceOutput.get("buffalo"));
}
示例8: select
import org.jruby.RubyHash; //导入依赖的package包/类
@JRubyMethod(module = true)
public static IRubyObject select(ThreadContext context, IRubyObject self, IRubyObject hash) {
Ruby runtime = context.runtime;
RubyHash cases = hash.convertToHash();
Set<RubyHash.RubyHashEntry> entries = (Set<RubyHash.RubyHashEntry>)cases.directEntrySet();
Object[] entryArray = entries.toArray();
OUTER: while (true) {
for (Object _entry : entryArray) {
RubyHash.RubyHashEntry entry = (RubyHash.RubyHashEntry)_entry;
IRubyObject _channel = (IRubyObject)entry.getKey();
if (!(_channel instanceof JoChannel)) throw runtime.newTypeError(_channel, runtime.getClassFromPath("Jo::Channel"));
IRubyObject _proc = (IRubyObject)entry.getValue();
IRubyObject value;
if ((value = ((JoChannel)_channel).queue.poll()) != null) {
_proc.callMethod(context, "call", value);
break OUTER;
}
Thread.yield();
}
}
return context.nil;
}
示例9: getTreeNodes
import org.jruby.RubyHash; //导入依赖的package包/类
private List<TreeNode> getTreeNodes(RubyArray allElements, UIElement root, String baseQuery, Set<UIElement> inspectedElements) throws CalabashException {
List<TreeNode> roots = new ArrayList<TreeNode>();
for (int i = allElements.size() - 1; i >= 0; i--) {
final String query = String.format(baseQuery + " index:%d", i);
RubyHash rubyElement = (RubyHash) allElements.get(i);
UIElement currentElement = new UIElement(rubyElement, query, calabashWrapper);
List<UIElement> uiElements = new ArrayList<UIElement>();
if (inspectedElements.contains(currentElement))
continue;
uiElements.add(currentElement);
uiElements.addAll(getAncestors(query, root));
merge(uiElements, roots);
inspectedElements.addAll(uiElements);
}
return roots;
}
示例10: UIElements
import org.jruby.RubyHash; //导入依赖的package包/类
public UIElements(RubyArray elements, String query, CalabashWrapper wrapper)
throws CalabashException {
query = query.trim();
Pattern pattern = Pattern.compile("^.+index:[0-9]+$");
Matcher matcher = pattern.matcher(query);
boolean indexedQuery = matcher.matches();
for (int i = 0; i < elements.size(); i++) {
try {
RubyHash object = (RubyHash) elements.get(i);
String q = query;
if (!indexedQuery)
q += " index:" + i;
this.add(new UIElement(object, q, wrapper));
} catch (Exception e) {
throw new CalabashException("Unsupported result format.\n" + elements.toString(), e);
}
}
}
示例11: convertToRubyHash
import org.jruby.RubyHash; //导入依赖的package包/类
private RubyHash convertToRubyHash(Set<Map.Entry<String, Object>> entries) {
RubyHash hash = newHash(runtime);
for (Map.Entry<String, Object> entry : entries) {
String key = entry.getKey();
Object value = entry.getValue();
if (key.equals("rack.input")) {
value = new JRubyRackInput(runtime, (RackInput) value);
}
if (key.equals("rack.version")) {
value = convertToRubyArray((List<Integer>) value);
}
hash.put(key, value);
}
return hash;
}
示例12: get_browser_caps
import org.jruby.RubyHash; //导入依赖的package包/类
public static RubyHash get_browser_caps(RubyHash caps) {
@SuppressWarnings("unchecked")
Map<String, Object> ourCaps = (Map<String, Object>) TestAttributes.get("capabilities");
if (ourCaps == null)
return new RubyHash(caps.getRuntime());
return map2hash(caps.getRuntime(), ourCaps);
}
示例13: valueToStringRepresentation
import org.jruby.RubyHash; //导入依赖的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();
}
示例14: applyEnvironment
import org.jruby.RubyHash; //导入依赖的package包/类
/**
* applyEnvironment
*
* @param container
*/
protected void applyEnvironment()
{
Ruby runtime = this.getRuntime();
IRubyObject env = runtime.getObject().getConstant(ENV_PROPERTY);
if (env != null && env instanceof RubyHash)
{
RubyHash hash = (RubyHash) env;
// save copy for later
this._originalEnvironment = (RubyHash) hash.dup();
hash.putAll(this.getContributedEnvironment());
// Grab all the matching env objects contributed via bundles that have scope matching!
IModelFilter filter = new ScopeFilter((String) hash.get("TM_CURRENT_SCOPE")); //$NON-NLS-1$
List<EnvironmentElement> envs = BundleManager.getInstance().getEnvs(filter);
ScopeSelector.sort(envs);
for (EnvironmentElement e : envs)
{
RubyProc invoke = e.getInvokeBlock();
if (invoke != null)
{
invoke.call(runtime.getCurrentContext(), new IRubyObject[] { hash });
}
}
}
}
示例15: test_deepClone
import org.jruby.RubyHash; //导入依赖的package包/类
@Test
public void test_deepClone() {
RubyHash rubyHash = (RubyHash) RubyUtil.evalScriptlet(getClass().getResourceAsStream("/order1.rb"));
Map<String,Object> deepClone = ServiceInvoker.deepClone(rubyHash);
Map header = (Map) deepClone.get("header");
Assert.assertFalse(header instanceof IRubyObject);
Assert.assertEquals(1234L, header.get("orderId"));
List items = (List) deepClone.get("items");
Assert.assertFalse(items instanceof IRubyObject);
Assert.assertEquals(2, items.size());
}