/// <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"); } }
private void AppendInspect() { MutableString inspect = RubySites.Inspect(_context, _opts.Value); if (KernelOps.Tainted(_context, inspect)) { _tainted = true; } AppendString(inspect); }
public static MutableString Inspect(RubyContext /*!*/ context, FileSystemInfo /*!*/ self) { string result = String.Format( "#<File::Stat dev={0}, ino={1}, mode={2}, nlink={3}, uid={4}, gid={5}, rdev={6}, size={7}, blksize={8}, blocks={9}, atime={10}, mtime={11}, ctime={12}", RubySites.Inspect(context, DeviceId(self)), RubySites.Inspect(context, Inode(self)), RubySites.Inspect(context, Mode(self)), RubySites.Inspect(context, NumberOfLinks(self)), RubySites.Inspect(context, UserId(self)), RubySites.Inspect(context, GroupId(self)), RubySites.Inspect(context, DeviceId(self)), RubySites.Inspect(context, Size(self)), RubySites.Inspect(context, BlockSize(self)), RubySites.Inspect(context, Blocks(self)), TimeOps.ToString(AccessTime(self)), TimeOps.ToString(ModifiedTime(self)), TimeOps.ToString(CreateTime(self)) ); return(MutableString.Create(result)); }
/// <summary> /// Step through a Range of Strings. /// </summary> /// <remarks> /// This method requires step to be a Fixnum. /// It uses a hybrid string comparison to prevent infinite loops and calls String#succ to get each item in the range. /// </remarks> private static object StepString(RubyContext /*!*/ context, BlockParam block, Range /*!*/ self, MutableString begin, MutableString end, int step) { CheckStep(step); object result; MutableString item = begin; int comp; while ((comp = Protocols.Compare(context, item, end)) < 0) { if (block == null) { throw RubyExceptions.NoBlockGiven(); } if (block.Yield(item, out result)) { return(result); } for (int i = 0; i < step; ++i) { item = (MutableString)RubySites.Successor(context, item); } if (item.Length > end.Length) { return(self); } } if (comp == 0 && !self.ExcludeEnd) { if (block == null) { throw RubyExceptions.NoBlockGiven(); } if (block.Yield(item, out result)) { return(result); } } return(self); }
public static bool Equals(RubyStruct /*!*/ self, object obj) { var other = obj as RubyStruct; if (!self.StructReferenceEquals(other)) { return(false); } Debug.Assert(self.ItemCount == other.ItemCount); for (int i = 0; i < self.Values.Length; i++) { if (!RubySites.Equal(self.Class.Context, self.Values[i], other.Values[i])) { return(false); } } return(true); }
public static RubyArray Grep(RubyContext /*!*/ context, BlockParam action, object self, object pattern) { RubyArray result = new RubyArray(); Each(context, self, Proc.Create(context, delegate(BlockParam /*!*/ selfBlock, object item) { if (RubySites.CaseEqual(context, pattern, item)) { if (action != null) { if (action.Yield(item, out item)) { return(item); } } result.Add(item); } return(null); })); return(result); }
public static object Equal(RubyContext /*!*/ context, object self, object other) { // Short circuit long winded comparison if the objects are actually the same. if (self == other) { return(true); } // TODO: handle exceptions thrown from Compare() // Compare may return null object result = RubySites.Compare(context, self, other); if (result is int) { return((int)result == 0); } else { return(null); } }
public static MutableString /*!*/ ToPrintedString(RubyContext /*!*/ context, object obj) { IDictionary <object, object> hash; List <object> list; MutableString str; if ((list = obj as List <object>) != null) { return(IListOps.Join(context, list, Environment.NewLine)); } else if ((hash = obj as IDictionary <object, object>) != null) { return(IDictionaryOps.ToString(context, hash)); } else if (obj == null) { return(MutableString.Create("nil")); } else if (obj is bool) { return(MutableString.Create((bool)obj ? "true" : "false")); } else if (obj is double) { var result = MutableString.Create(obj.ToString()); if ((double)(int)(double)obj == (double)obj) { result.Append(".0"); } return(result); } else if ((str = obj as MutableString) != null) { return(str); } else { return(RubySites.ToS(context, obj)); } }
public static MutableString Inspect(RubyContext /*!*/ context, IDictionary <object, object> /*!*/ self) { using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { if (handle == null) { return(MutableString.Create("{...}")); } MutableString str = MutableString.CreateMutable(); str.Append('{'); foreach (KeyValuePair <object, object> pair in self) { if (str.Length != 1) { str.Append(", "); } str.Append(RubySites.Inspect(context, BaseSymbolDictionary.ObjToNull(pair.Key))); str.Append("=>"); str.Append(RubySites.Inspect(context, pair.Value)); } str.Append('}'); return(str); } }
/// <summary> /// Step through a Range of objects that are not Numeric or String. /// </summary> private static object StepObject(RubyContext /*!*/ context, BlockParam block, Range /*!*/ self, object begin, object end, int step) { CheckStep(context, step); object item = begin, result; int comp; while ((comp = Protocols.Compare(context, item, end)) < 0) { if (block == null) { throw RubyExceptions.NoBlockGiven(); } if (block.Yield(item, out result)) { return(result); } for (int i = 0; i < step; ++i) { item = RubySites.Successor(context, item); } } if (comp == 0 && !self.ExcludeEnd) { if (block == null) { throw RubyExceptions.NoBlockGiven(); } if (block.Yield(item, out result)) { return(result); } } return(self); }
public static object Find(RubyContext /*!*/ context, BlockParam predicate, object self, [Optional] object ifNone) { object result = Missing.Value; Each(context, self, Proc.Create(context, delegate(BlockParam /*!*/ selfBlock, object item) { if (predicate == null) { throw RubyExceptions.NoBlockGiven(); } object blockResult; if (predicate.Yield(item, out blockResult)) { result = blockResult; return(selfBlock.Break(blockResult)); } if (Protocols.IsTrue(blockResult)) { result = item; return(selfBlock.Break(item)); } return(null); })); if (result == Missing.Value) { if (ifNone == Missing.Value || ifNone == null) { return(null); } result = RubySites.Call(context, ifNone); } return(result); }
public static object InducedFrom(RubyClass /*!*/ self, double obj) { return(RubySites.ToI(self.Context, obj)); }
public static double InducedFrom(RubyClass /*!*/ self, [NotNull] BigInteger /*!*/ value) { return(RubySites.ToF(self.Context, value)); }
public static double InducedFrom(RubyClass /*!*/ self, int value) { return(RubySites.ToF(self.Context, value)); }
internal string /*!*/ GetDebugView() { return(RubySites.Inspect(RubyContext._Default, this).ToString()); }
public static object ToInt(RubyContext /*!*/ context, object self) { return(RubySites.ToI(context, self)); }