Exemple #1
0
        public static object DownTo(RubyContext /*!*/ context, BlockParam block, object /*!*/ self, object other)
        {
            object i       = self;
            object compare = null;

            while (RubyOps.IsFalse(compare))
            {
                // Rather than test i >= other we test !(i < other)
                compare = LibrarySites.LessThan(context, i, other);

                // If the comparison failed (i.e. returned null) then we throw an error.
                if (compare == null)
                {
                    throw RubyExceptions.MakeComparisonError(context, i, other);
                }

                // If the comparison worked but returned false then we
                if (RubyOps.IsFalse(compare))
                {
                    object result;
                    if (block == null)
                    {
                        throw RubyExceptions.NoBlockGiven();
                    }

                    if (block.Yield(i, out result))
                    {
                        return(result);
                    }

                    i = LibrarySites.Subtract(context, i, 1);
                }
            }
            return(self);
        }
Exemple #2
0
 public static object Abs(RubyContext /*!*/ context, object self)
 {
     if (RubyOps.IsTrue(LibrarySites.LessThan(context, self, 0)))
     {
         return(LibrarySites.UnaryMinus(context, self));
     }
     return(self);
 }
Exemple #3
0
 /// <summary>
 /// Check that the object, when converted to an integer, is not less than or equal to zero.
 /// </summary>
 private static void CheckStep(RubyContext /*!*/ context, object step)
 {
     if (RubySites.Equal(context, step, 0))
     {
         throw RubyExceptions.CreateArgumentError("step can't be 0");
     }
     if (RubyOps.IsTrue(LibrarySites.LessThan(context, step, 0)))
     {
         throw RubyExceptions.CreateArgumentError("step can't be negative");
     }
 }
Exemple #4
0
        public static object Remainder(RubyContext /*!*/ context, object self, object other)
        {
            object modulo = LibrarySites.Modulo(context, self, other);

            if (!Protocols.IsEqual(context, modulo, 0))
            {
                // modulo is not zero
                if (RubyOps.IsTrue(LibrarySites.LessThan(context, self, 0)) && RubyOps.IsTrue(LibrarySites.GreaterThan(context, other, 0)) ||
                    RubyOps.IsTrue(LibrarySites.GreaterThan(context, self, 0)) && RubyOps.IsTrue(LibrarySites.LessThan(context, other, 0)))
                {
                    // (self is negative and other is positive) OR (self is positive and other is negative)
                    return(LibrarySites.Minus(context, modulo, other));
                }
            }
            // Either modulo is zero or self and other are not of the same sign
            return(modulo);
        }
Exemple #5
0
        public static object Times(RubyContext /*!*/ context, BlockParam block, object /*!*/ self)
        {
            object i = 0;

            while (RubyOps.IsTrue(LibrarySites.LessThan(context, i, self)))
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                object result;
                if (block.Yield(i, out result))
                {
                    return(result);
                }

                i = LibrarySites.Add(context, i, 1);
            }
            return(self);
        }