Search in sources :

Example 1 with RubyRange

use of org.jruby.RubyRange in project nokogiri by sparklemotion.

the class XmlNodeSet method rangeBeginLength.

// replace with
// https://github.com/jruby/jruby/blame/13a3ec76d883a162b9d46c374c6e9eeea27b3261/core/src/main/java/org/jruby/RubyRange.java#L974
// once we upgraded the min JRuby version to >= 9.2
private static IRubyObject rangeBeginLength(ThreadContext context, IRubyObject rangeMaybe, int len, int[] begLen) {
    RubyRange range = (RubyRange) rangeMaybe;
    int min = range.begin(context).convertToInteger().getIntValue();
    int max = range.end(context).convertToInteger().getIntValue();
    if (min < 0) {
        min += len;
        if (min < 0) {
            throw context.runtime.newRangeError(min + ".." + (range.isExcludeEnd() ? "." : "") + max + " out of range");
        }
    }
    if (max < 0) {
        max += len;
    }
    if (!range.isExcludeEnd()) {
        max++;
    }
    begLen[0] = min;
    begLen[1] = max;
    return context.tru;
}
Also used : RubyRange(org.jruby.RubyRange)

Example 2 with RubyRange

use of org.jruby.RubyRange in project propane by ruby-processing.

the class MathToolModule method mapOneD.

/**
 * @param context ThreadContext
 * @param recv IRubyObject
 * @param args array of RubyRange (must be be numeric)
 * @return mapped value RubyFloat
 */
@JRubyMethod(name = "map1d", rest = true, module = true)
public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
    double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
    RubyRange r1 = (RubyRange) args[1];
    RubyRange r2 = (RubyRange) args[2];
    double first1 = r1.first(context) instanceof RubyFloat ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
    double first2 = r2.first(context) instanceof RubyFloat ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
    double last1 = r1.last(context) instanceof RubyFloat ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
    double last2 = r2.last(context) instanceof RubyFloat ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
    return mapMt(context, value, first1, last1, first2, last2);
}
Also used : RubyRange(org.jruby.RubyRange) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with RubyRange

use of org.jruby.RubyRange in project propane by ruby-processing.

the class MathToolModule method constrainedMap.

/**
 * @param context ThreadContext
 * @param recv IRubyObject
 * @param args array of RubyRange (must be be numeric)
 * @return mapped value RubyFloat
 */
@JRubyMethod(name = "constrained_map", rest = true, module = true)
public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
    double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
    RubyRange r1 = (RubyRange) args[1];
    RubyRange r2 = (RubyRange) args[2];
    double first1 = r1.first(context) instanceof RubyFloat ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
    double first2 = r2.first(context) instanceof RubyFloat ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
    double last1 = r1.last(context) instanceof RubyFloat ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
    double last2 = r2.last(context) instanceof RubyFloat ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
    double max = Math.max(first1, last1);
    double min = Math.min(first1, last1);
    if (value < min) {
        return mapMt(context, min, first1, last1, first2, last2);
    }
    if (value > max) {
        return mapMt(context, max, first1, last1, first2, last2);
    }
    return mapMt(context, value, first1, last1, first2, last2);
}
Also used : RubyRange(org.jruby.RubyRange) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

RubyRange (org.jruby.RubyRange)3 RubyFloat (org.jruby.RubyFloat)2 JRubyMethod (org.jruby.anno.JRubyMethod)2