use of org.jruby.RubyHash in project webmagic by code4craft.
the class ScriptProcessor method process.
@Override
public void process(Page page) {
ScriptEngine engine = enginePool.getEngine();
try {
ScriptContext context = engine.getContext();
context.setAttribute("page", page, ScriptContext.ENGINE_SCOPE);
context.setAttribute("config", site, ScriptContext.ENGINE_SCOPE);
try {
switch(language) {
case JavaScript:
engine.eval(defines + "\n" + script, context);
// }
break;
case JRuby:
RubyHash oRuby = (RubyHash) engine.eval(defines + "\n" + script, context);
Iterator itruby = oRuby.entrySet().iterator();
while (itruby.hasNext()) {
Map.Entry pairs = (Map.Entry) itruby.next();
page.getResultItems().put(pairs.getKey().toString(), pairs.getValue());
}
break;
case Jython:
engine.eval(defines + "\n" + script, context);
PyDictionary oJython = (PyDictionary) engine.get("result");
Iterator it = oJython.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
page.getResultItems().put(pairs.getKey().toString(), pairs.getValue());
}
break;
}
} catch (ScriptException e) {
e.printStackTrace();
}
} finally {
enginePool.release(engine);
}
}
use of org.jruby.RubyHash in project enumerable by hraberg.
the class JRubyTestBase method javaInRubyToRuby.
protected static IRubyObject javaInRubyToRuby(Ruby ruby, Object value) {
if (value instanceof List<?> && !(value instanceof RubyArray)) {
RubyArray array = RubyArray.newArray(ruby);
array.addAll((Collection<?>) value);
return array;
}
if (value instanceof Map<?, ?> && !(value instanceof RubyHash)) {
RubyHash hash = RubyHash.newHash(ruby);
hash.putAll((Map<?, ?>) value);
return hash;
}
return javaToRuby(ruby, value);
}
use of org.jruby.RubyHash in project nokogiri by sparklemotion.
the class ReaderNode method getAttributes.
public IRubyObject getAttributes(ThreadContext context) {
final Ruby runtime = context.runtime;
if (attributeList == null)
return runtime.getNil();
RubyHash hash = RubyHash.newHash(runtime);
for (int i = 0; i < attributeList.length; i++) {
IRubyObject k = stringOrBlank(runtime, attributeList.names.get(i));
IRubyObject v = stringOrBlank(runtime, attributeList.values.get(i));
// hash.op_aset(context, k, v)
hash.fastASetCheckString(runtime, k, v);
}
return hash;
}
use of org.jruby.RubyHash in project gocd by gocd.
the class ReaderNode method getAttributes.
public IRubyObject getAttributes(ThreadContext context) {
if (attributeList == null)
return context.getRuntime().getNil();
RubyHash hash = RubyHash.newHash(context.getRuntime());
for (int i = 0; i < attributeList.length; i++) {
IRubyObject k = stringOrBlank(context.getRuntime(), attributeList.names.get(i));
IRubyObject v = stringOrBlank(context.getRuntime(), attributeList.values.get(i));
if (context.getRuntime().is1_9())
hash.op_aset19(context, k, v);
else
hash.op_aset(context, k, v);
}
return hash;
}
use of org.jruby.RubyHash in project gocd by gocd.
the class ReaderNode method getNamespaces.
public IRubyObject getNamespaces(ThreadContext context) {
if (namespaces == null)
return ruby.getNil();
RubyHash hash = RubyHash.newHash(ruby);
Set<String> keys = namespaces.keySet();
for (String key : keys) {
String stringValue = namespaces.get(key);
IRubyObject k = stringOrBlank(context.getRuntime(), key);
IRubyObject v = stringOrBlank(context.getRuntime(), stringValue);
if (context.getRuntime().is1_9())
hash.op_aset19(context, k, v);
else
hash.op_aset(context, k, v);
}
return hash;
}
Aggregations