Exemple #1
0
        public static object UpTo(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.GreaterThan(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 carry on
                if (RubyOps.IsFalse(compare))
                {
                    if (block == null)
                    {
                        throw RubyExceptions.NoBlockGiven();
                    }

                    object result;
                    if (block.Yield(i, out result))
                    {
                        return(result);
                    }
                    i = LibrarySites.Add(context, i, 1);
                }
            }
            return(self);
        }
Exemple #2
0
        /// <summary>
        /// Step through a Range of Numerics.
        /// </summary>
        /// <remarks>
        /// </remarks>
        private static object StepNumeric(RubyContext /*!*/ context, BlockParam block, Range /*!*/ self, object begin, object end, object step)
        {
            CheckStep(context, step);

            object item = begin;

            Protocols.DynamicInvocation compareOp;
            if (self.ExcludeEnd)
            {
                compareOp = LibrarySites.LessThan;
            }
            else
            {
                compareOp = LibrarySites.LessThanOrEqual;
            }

            object result;

            while (RubyOps.IsTrue(compareOp(context, item, end)))
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

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

                item = LibrarySites.Add(context, item, step);
            }

            return(self);
        }
Exemple #3
0
 public static object Step(RubyContext /*!*/ context, BlockParam block, object self, object limit, object step)
 {
     if (self is double || limit is double || step is double)
     {
         // At least one of the arguments is double so convert all to double and run the Float version of Step
         double floatSelf  = Protocols.ConvertToFloat(context, self);
         double floatLimit = Protocols.ConvertToFloat(context, limit);
         double floatStep  = Protocols.ConvertToFloat(context, step);
         return(Step(context, block, floatSelf, floatLimit, floatSelf));
     }
     else
     {
         #region The generic step algorithm:
         // current = self
         // if step is postive then
         //   while current < limit do
         //     yield(current)
         //     current = current + step
         //   end
         // else
         //   while current > limit do
         //     yield(current)
         //     current = current + step
         //   end
         // return self
         #endregion
         bool isStepZero = Protocols.IsEqual(context, step, 0);
         if (isStepZero)
         {
             throw RubyExceptions.CreateArgumentError("step can't be 0");
         }
         bool isStepPositive = RubyOps.IsTrue(LibrarySites.GreaterThan(context, step, 0));
         Protocols.DynamicInvocation compare;
         if (isStepPositive)
         {
             compare = LibrarySites.GreaterThan;
         }
         else
         {
             compare = LibrarySites.LessThan;
         }
         object current = self;
         while (!RubyOps.IsTrue(compare.Invoke(context, current, limit)))
         {
             object result;
             if (YieldStep(block, current, out result))
             {
                 return(result);
             }
             current = LibrarySites.Add(context, current, step);
         }
         return(self);
     }
 }
Exemple #4
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);
        }
Exemple #5
0
 public static object Next(RubyContext /*!*/ context, object /*!*/ self)
 {
     return(LibrarySites.Add(context, self, 1));
 }