public static object Partition(CallSiteStorage <EachSite> /*!*/ each, [NotNull] BlockParam /*!*/ predicate, object self) { RubyArray trueSet = new RubyArray(); RubyArray falseSet = new RubyArray(); RubyArray pair = new RubyArray(2); pair.Add(trueSet); pair.Add(falseSet); object result = pair; Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { object blockResult; if (predicate.Yield(item, out blockResult)) { result = blockResult; return(selfBlock.PropagateFlow(predicate, blockResult)); } if (Protocols.IsTrue(blockResult)) { trueSet.Add(item); } else { falseSet.Add(item); } return(null); })); return(result); }
public static RubyArray /*!*/ GetAllNames(RubyContext /*!*/ context, RubyEncoding /*!*/ self) { var result = new RubyArray(); string name = self.Name; result.Add(MutableString.Create(name)); foreach (var alias in RubyEncoding.Aliases) { if (StringComparer.OrdinalIgnoreCase.Equals(alias.Value, name)) { result.Add(MutableString.CreateAscii(alias.Key)); } } if (self == context.RubyOptions.LocaleEncoding) { result.Add(MutableString.CreateAscii("locale")); } if (self == context.DefaultExternalEncoding) { result.Add(MutableString.CreateAscii("external")); } if (self == context.GetPathEncoding()) { result.Add(MutableString.CreateAscii("filesystem")); } return(result); }
public static RubyArray /*!*/ Partition(RubyContext /*!*/ context, BlockParam predicate, object self) { RubyArray trueSet = new RubyArray(); RubyArray falseSet = new RubyArray(); 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)) { return(blockResult); } if (Protocols.IsTrue(blockResult)) { trueSet.Add(item); } else { falseSet.Add(item); } return(null); })); RubyArray pair = new RubyArray(2); pair.Add(trueSet); pair.Add(falseSet); return(pair); }
internal static RubyArray /*!*/ MakeArray(object key, object value) { RubyArray list = new RubyArray(2); list.Add(CustomStringDictionary.ObjToNull(key)); list.Add(value); return(list); }
public static RubyArray Split(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path) { RubyArray result = new RubyArray(2); result.Add(DirName(self, path)); result.Add(Basename(self, path, null)); return(result); }
// Make a 2 element array internal static RubyArray /*!*/ MakeArray(KeyValuePair <object, object> pair) { RubyArray list = new RubyArray(2); list.Add(CustomStringDictionary.ObjToNull(pair.Key)); list.Add(pair.Value); return(list); }
public static object DefineFinalizer(RubyModule /*!*/ self, object obj, Proc proc) { RubyArray result = new RubyArray(2); result.Add(0); result.Add(proc); return(result); }
public static RubyArray /*!*/ OpenPipe(RubyClass /*!*/ self) { Stream reader, writer; RubyPipe.CreatePipe(out reader, out writer); RubyArray result = new RubyArray(2); result.Add(new RubyIO(self.Context, reader, IOMode.ReadOnly)); result.Add(new RubyIO(self.Context, writer, IOMode.WriteOnly)); return(result); }
public static object DefineFinalizer(RespondToStorage /*!*/ respondTo, BinaryOpStorage /*!*/ call, RubyModule /*!*/ self, object obj, object finalizer) { if (!Protocols.RespondTo(respondTo, finalizer, "call")) { throw RubyExceptions.CreateArgumentError("finalizer should be callable (respond to :call)"); } respondTo.Context.SetInstanceVariable(obj, FinalizerInvoker.InstanceVariableName, new FinalizerInvoker(call.GetCallSite("call"), finalizer)); RubyArray result = new RubyArray(2); result.Add(0); result.Add(finalizer); return(result); }
public static RubyArray /*!*/ GetAvailableEncodings(RubyClass /*!*/ self) { var infos = Encoding.GetEncodings(); var result = new RubyArray(1 + infos.Length); // Ruby specific: result.Add(RubyEncoding.Binary); foreach (var info in infos) { result.Add(RubyEncoding.GetRubyEncoding(info.CodePage)); } return(result); }
public static RubyArray Frexp(object self, [DefaultProtocol] double x) { byte[] bytes; double mantissa; int exponent; RubyArray result = new RubyArray(2); bytes = System.BitConverter.GetBytes(x); mantissa = (Mantissa(bytes) * SM.Pow(2, -52) + 1.0) / 2; exponent = Exponent(bytes) - 1022; result.Add(mantissa); result.Add(exponent); return(result); }
public static RubyArray /*!*/ Zip(RubyContext /*!*/ context, BlockParam block, object self, [NotNull] params object[] args) { RubyArray results = (block == null) ? new RubyArray() : null; // Call to_a on each argument IList[] otherArrays = new IList[args.Length]; for (int i = 0; i < args.Length; i++) { otherArrays[i] = Protocols.ConvertToArray(context, args[i]); } int index = 0; Each(context, self, Proc.Create(context, delegate(BlockParam /*!*/ selfBlock, object item) { // Collect items RubyArray array = new RubyArray(otherArrays.Length + 1); array.Add(item); foreach (IList otherArray in otherArrays) { if (index < otherArray.Count) { array.Add(otherArray[index]); } else { array.Add(null); } } index += 1; if (block != null) { object blockResult; if (block.Yield(array, out blockResult)) { return(blockResult); } } else { results.Add(array); } return(null); })); return(results); }
public static object Grep(CallSiteStorage <EachSite> /*!*/ each, BinaryOpStorage /*!*/ caseEquals, BlockParam action, object self, object pattern) { RubyArray resultArray = new RubyArray(); object result = resultArray; var site = caseEquals.GetCallSite("==="); Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { if (RubyOps.IsTrue(site.Target(site, pattern, item))) { if (action != null) { if (action.Yield(item, out item)) { result = item; return(selfBlock.PropagateFlow(action, item)); } } resultArray.Add(item); } return(null); })); return(result); }
private static RubyArray /*!*/ Filter(RubyContext /*!*/ context, BlockParam predicate, object self, bool acceptingValue) { RubyArray result = new RubyArray(); 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)) { return(blockResult); } // Check if the result is what we expect (use true to select, false to reject) if (Protocols.IsTrue(blockResult) == acceptingValue) { result.Add(item); } return(null); })); return(result); }
public static object TakeWhile(CallSiteStorage <EachSite> /*!*/ each, [NotNull] BlockParam /*!*/ predicate, object self) { RubyArray resultArray = new RubyArray(); object result = resultArray; Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { object blockResult; if (predicate.Yield(item, out blockResult)) { result = blockResult; return(selfBlock.PropagateFlow(predicate, blockResult)); } if (Protocols.IsTrue(blockResult)) { resultArray.Add(item); } else { selfBlock.Break(null); } return(null); })); return(result); }
public static RubyArray /*!*/ Take(CallSiteStorage <EachSite> /*!*/ each, object self, [DefaultProtocol] int count) { if (count < 0) { throw RubyExceptions.CreateArgumentError("attempt to take negative size"); } var result = new RubyArray(count); if (count == 0) { return(result); } Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { result.Add(item); if (--count == 0) { selfBlock.Break(null); } return(null); })); return(result); }
public static RubyArray /*!*/ GetDefinedConstants(RubyModule /*!*/ self) { var visited = new Dictionary <string, bool>(); var result = new RubyArray(); bool hideGlobalConstants = !self.IsObjectClass; using (self.Context.ClassHierarchyLocker()) { self.ForEachConstant(true, delegate(RubyModule /*!*/ module, string name, object value) { if (name == null) { // terminate enumeration when Object is reached return(hideGlobalConstants && module.IsObjectClass); } if (!visited.ContainsKey(name)) { if (Tokenizer.IsConstantName(name, true)) { result.Add(self.Context.StringifyIdentifier(name)); } visited.Add(name, true); } return(false); }); } return(result); }
public static Hash GroupBy(CallSiteStorage <EachSite> /*!*/ each, [NotNull] BlockParam /*!*/ predicate, object self) { var grouped = new Dictionary <object, object>(); Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { object blockResult; if (predicate.Yield(item, out blockResult)) { return(selfBlock.PropagateFlow(predicate, blockResult)); } RubyArray existingGroup = IDictionaryOps.GetElement(each.Context, grouped, blockResult) as RubyArray; if (existingGroup != null) { existingGroup.Add(item); } else { IDictionaryOps.SetElement(each.Context, grouped, blockResult, new RubyArray { item }); } return(null); })); return(new Hash(grouped)); }
private static object Filter(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self, bool acceptingValue) { RubyArray resultArray = new RubyArray(); object result = resultArray; Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { if (predicate == null) { throw RubyExceptions.NoBlockGiven(); } object blockResult; if (predicate.Yield(item, out blockResult)) { result = blockResult; return(selfBlock.PropagateFlow(predicate, blockResult)); } // Check if the result is what we expect (use true to select, false to reject) if (Protocols.IsTrue(blockResult) == acceptingValue) { resultArray.Add(item); } return(null); })); return(result); }
public static object DeleteIf([NotNull] BlockParam /*!*/ block, IDictionary <object, object> /*!*/ self) { // Make a copy of the keys to delete, so we don't modify the collection // while iterating over it RubyArray keysToDelete = new RubyArray(); foreach (var pair in self) { object result; if (block.Yield(CustomStringDictionary.ObjToNull(pair.Key), pair.Value, out result)) { return(result); } // Delete the key, unless 'false' or 'nil' is returned if (RubyOps.IsTrue(result)) { keysToDelete.Add(pair.Key); } } foreach (object key in keysToDelete) { self.Remove(key); } return(self); }
public static RubyArray ToArray(DateTime /*!*/ self) { RubyArray result = new RubyArray(); result.Add(self.Second); result.Add(self.Minute); result.Add(self.Hour); result.Add(self.Day); result.Add(self.Month); result.Add(self.Year); result.Add((int)self.DayOfWeek); result.Add(self.DayOfYear); result.Add(self.IsDaylightSavingTime()); result.Add(GetZone(self)); return(result); }
public static object RejectMutate(RubyContext /*!*/ context, BlockParam block, IDictionary <object, object> /*!*/ self) { RubyUtils.RequiresNotFrozen(context, self); // Make a copy of the keys to delete, so we don't modify the collection // while iterating over it RubyArray keysToDelete = new RubyArray(); foreach (KeyValuePair <object, object> pair in self) { object result; if (block.Yield(CustomStringDictionary.ObjToNull(pair.Key), pair.Value, out result)) { return(result); } // Delete the key, unless 'false' or 'nil' is returned if (RubyOps.IsTrue(result)) { keysToDelete.Add(pair.Key); } } foreach (object key in keysToDelete) { self.Remove(key); } return(keysToDelete.Count == 0 ? null : self); }
public static object DeleteIf(RubyContext /*!*/ context, BlockParam block, IDictionary <object, object> /*!*/ self) { RubyUtils.RequiresNotFrozen(context, self); if (self.Count > 0 && block == null) { throw RubyExceptions.NoBlockGiven(); } // Make a copy of the keys to delete, so we don't modify the collection // while iterating over it RubyArray keysToDelete = new RubyArray(); foreach (var pair in self) { object result; if (block.Yield(BaseSymbolDictionary.ObjToNull(pair.Key), pair.Value, out result)) { return(result); } // Delete the key, unless 'false' or 'nil' is returned if (RubyOps.IsTrue(result)) { keysToDelete.Add(pair.Key); } } foreach (object key in keysToDelete) { self.Remove(key); } return(self); }
public static RubyArray /*!*/ GetAvailableEncodings(RubyClass /*!*/ self) { // TODO: loads all encodings, we should be lazy with encoding creation var infos = Encoding.GetEncodings(); var result = new RubyArray(1 + infos.Length); // Ruby specific: result.Add(RubyEncoding.Binary); foreach (var info in infos) { result.Add(RubyEncoding.GetRubyEncoding(info.GetEncoding())); } return(result); }
public static RubyArray ToArray(RubyContext /*!*/ context, RubyTime /*!*/ self) { RubyArray result = new RubyArray(); result.Add(self.DateTime.Second); result.Add(self.DateTime.Minute); result.Add(self.DateTime.Hour); result.Add(self.DateTime.Day); result.Add(self.DateTime.Month); result.Add(self.DateTime.Year); result.Add((int)self.DateTime.DayOfWeek); result.Add(self.DateTime.DayOfYear); result.Add(self.GetCurrentDst(context)); result.Add(GetZone(context, self)); return(result); }
public static RubyArray /*!*/ Create(object item) { var result = new RubyArray(); result.Add(item); return(result); }
public static RubyArray /*!*/ Offset(MatchData /*!*/ self, [DefaultProtocol] int groupIndex) { self.RequireExistingGroup(groupIndex); RubyArray result = new RubyArray(2); if (self.GroupSuccess(groupIndex)) { result.Add(self.GetGroupStart(groupIndex)); result.Add(self.GetGroupEnd(groupIndex)); } else { result.Add(null); result.Add(null); } return(result); }
public static RubyArray /*!*/ Offset(MatchData /*!*/ self, [DefaultProtocol] int groupIndex) { var group = self.GetExistingGroup(groupIndex); RubyArray result = new RubyArray(2); if (group.Success) { result.Add(group.Index); result.Add(group.Index + group.Length); } else { result.Add(null); result.Add(null); } return(result); }
private static RubyArray SelectInternal(RubyContext /*!*/ context, RubyArray read, RubyArray write, RubyArray error, TimeSpan timeout) { WaitHandle[] handles = null; RubyArray result; if (read == null && write == null && error == null) { Thread.Sleep(timeout); return(null); } try { handles = GetWaitHandles(context, read, write, error); int index; try { #if SILVERLIGHT index = WaitHandle.WaitAny(handles, timeout); #else index = WaitHandle.WaitAny(handles, timeout, false); #endif if (index == WaitHandle.WaitTimeout) { return(null); } } catch (Exception e) { throw new Errno.InvalidError(e.Message, e); } result = new RubyArray(); int handleIndex = 0; result.Add(MakeResult(handles, ref handleIndex, index, read)); result.Add(MakeResult(handles, ref handleIndex, index, write)); result.Add(MakeResult(handles, ref handleIndex, index, error)); } finally { // should we close the handles? //if (handles != null) { // for (int i = 0; i < handles.Length; i++) { // if (handles[i] != null) { // handles[i].Close(); // } // } //} } return(result); }
internal static RubyArray /*!*/ GetMethods(RubyModule /*!*/ self, bool inherited, RubyMethodAttributes attributes) { var result = new RubyArray(); self.ForEachMember(inherited, attributes, delegate(string /*!*/ name, RubyMemberInfo /*!*/ member) { result.Add(MutableString.Create(name)); }); return(result); }