Repository navigation
101 LINQ samples: using System::Linq, to_seq as a refinement, blocks, and CI runs them - #5
Merged
sholden merged 4 commits intoOct 29, 2026
Conversation
sholden
force-pushed
the
fix/linq-samples-scoped-using
branch
from
October 29, 2026 15:19
3178c86 to
9195092
Compare
using System::Linq, and CI runs themusing System::Linq, to_seq as a refinement, and CI runs them
Src/Samples/Linq/101samples.rb stopped parsing when Ruby stopped accepting a trailing comma in a nested block parameter, |(c,), p|, and nothing ran it after that. Past the parse error it raised a NameError at FirstOrDefault, which names Fixnum, gone since Ruby 3.2. - |(c,), p| is |(c, _), p|. - Fixnum is Integer, which is System::Int32 in IronRuby. - Min: Projection called sum, copied from Sum: Projection, and printed the total length of the words instead of the shortest. It calls min now, through method(:min).of(Object, Integer) as Min: Grouped does. A Func<Object, int> fits both Min(Func<TSource, int>) and Min<TSource, TResult>(Func<TSource, TResult>), and IronRuby can't choose between them where C# picks the first. - Repeat printed its 7s without a newline, so the next heading ran on. - OfType was commented out as an IronRuby bug. It works now. The file still calls using_clr_extensions. Its output is the reference for moving it to `using'. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The samples were the last code outside the tests that called using_clr_extensions, so they printed its deprecation warning. They say `using System::Linq' now. Inside the scope calls resolve as they did under using_clr_extensions, so every call keeps its name, and the output is the same apart from the warning. to_seq, which turns the samples' arrays into the IEnumerable<T> LINQ extends, was a method added to Object for the whole program. It is a refinement of Object now, active below the `using' like the LINQ methods. Nothing in the file calls it before that point. A probe at each of the 119 call sites with a LINQ method's name saw all of them run: 112 reach a System.Linq.Enumerable method, and the other 7 are Array#join on what to_a returned. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Nothing ran Src/Samples/Linq/101samples.rb, so nobody noticed when it stopped parsing. build-and-test now runs it on each OS and target framework, after the socket check, and diffs its stdout and stderr against Src/Samples/Linq/101samples.expected, which holds this branch's output on Linux and net10.0. A wrong answer, an exception or a warning fails the step. Tried locally by putting using_clr_extensions back, which adds its deprecation warning; by changing count(pred) to where(pred); by raising in the middle; and by dropping `using LinqSampleHelpers', which makes to_seq undefined. The sample takes about 1.6 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
sholden
force-pushed
the
fix/linq-samples-scoped-using
branch
from
October 29, 2026 15:33
9195092 to
621d38b
Compare
The samples passed every LINQ delegate as lambda { ... }. A block goes to a
method's trailing delegate parameter, so a call whose last argument was a
lambda now takes a block:
numbers.where { |n| n < 5 }, group_by(->(p) { p.category }) { |c, g| ... },
aggregate(start_balance) { |balance, next_withdrawal| ... }. A lambda that
isn't the last argument, such as a key selector before a result selector,
and the make_pair and identity helpers are -> lambdas now.
Two indexed samples keep a lambda. On .NET 9 and later OfType returns one
of LINQ's private iterator classes, whose own public Where(Func<T, bool>)
and Select(Func<T, R>) IronRuby binds to. A block with two parameters fits
those too, because a proc takes any number of arguments, so
digits.where { |digit, index| ... } got nil for index and
numbers.select { |num, index| ... } was ambiguous. A lambda's arity rules
them out. The indexed select_many, take_while and skip_while have no such
methods to compete with and take blocks. Each of the two has a comment.
The output is unchanged, so 101samples.expected is too. A probe at each of
the 119 call sites with a LINQ method's name still sees all of them reach
the same methods as before.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sholden
force-pushed
the
fix/linq-samples-scoped-using
branch
from
October 29, 2026 15:44
5263f16 to
0901891
Compare
using System::Linq, to_seq as a refinement, and CI runs themusing System::Linq, to_seq as a refinement, blocks, and CI runs them
This was referenced Oct 29, 2026
This was referenced Oct 29, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Src/Samples/Linq/101samples.rb, IronRuby's port of Microsoft's 101 LINQ Samples, was the last code outside the tests that calledusing_clr_extensions, so since #3 it printed the deprecation warning. It saysusing System::Linqnow. It also hadn't parsed in a long time. So this PR first gets it running under the old form, keeps that output as the reference, and then moves it tousing.It didn't run
Lines 578 and 586 used
|(c,), p|, which current Ruby rejects, and IronRuby does too. With that fixed, the file got as far as "FirstOrDefault: Simple" and raised a NameError, becauseFixnumhas been gone since Ruby 3.2. Nothing else raised. The first commit fixes both, plus three samples that ran but were wrong:|(c,), p|is|(c, _), p|.FixnumisInteger, which isSystem::Int32in IronRuby.sum, copied from "Sum: Projection", and printed 25, the total length of the words. It callsminnow and prints 5. It goes throughmethod(:min).of(Object, Integer), as "Min: Grouped" already did, becausewords.min(System::Func[Object, Integer].new { ... })raisesAmbiguousMatchExceptionunder either form. See "Found, not fixed".7777777777without a newline, so the next heading ran on.# TODO: IronRuby bug. It works under both forms now, so it's back.That commit still calls
using_clr_extensions. Its output is the reference.The move to
usingSince #3's last two commits, a call inside a
using System::Linqscope resolves exactly as it did underusing_clr_extensions. So the second commit changes theusingline and nothing else about the calls:select,first,count,group_byand the rest keep their names and still reach LINQ.It also turns
to_seq, which makes the samples' Ruby arrays into theIEnumerable<T>LINQ extends, from a method added toObjectfor the whole program into a refinement:Nothing in the file calls
to_seqbefore those lines. The two classes the file defines,ComparerandAnagramEqualityComparer, don't call it at all.Blocks instead of lambdas
The third commit passes the LINQ delegates as blocks where it can, since a block goes to a method's trailing delegate parameter:
numbers.where { |n| n < 5 },group_by(->(p) { p.category }) { |c, g| ... },aggregate(start_balance) { |balance, next_withdrawal| ... }. A lambda that isn't the last argument, such as a key selector before a result selector, is a->lambda now, and so aremake_pairandidentity.Two indexed samples keep a lambda, "Where: Indexed" and "Select: Indexed". Their receiver comes from
OfType, and on .NET 9 and later it has aWhere(Func<T, bool>)and aSelect(Func<T, R>)of its own (see "Found, not fixed"). A block with two parameters fits those too, because a proc takes any number of arguments. Sodigits.where { |digit, index| ... }got nil forindex, andnumbers.select { |num, index| ... }raisedAmbiguousMatchException. A lambda's arity rules them out. The indexedselect_many,take_whileandskip_whilehave no such methods competing and take blocks. Each of the two lambdas has a comment saying why.The output didn't change.
CI
build-and-testruns the sample after the socket check, on all four OS and framework legs, and diffs its stdout and stderr against the newSrc/Samples/Linq/101samples.expected. It takes about 1.6 s. A wrong answer, an exception or a warning fails the step. Locally, each of these made it fail: puttingusing_clr_extensionsback, changingcount(pred)towhere(pred), adding araise, and droppingusing LinqSampleHelpers. The comment in ci.yml gives the command that regenerates the file.Nothing outside
Src/IronRuby.Testsrecommendsusing_clr_extensionsany more. The README calls it deprecated, and the other mentions are its implementation.What I ran
All on Linux, net10.0, Debug, on top of df7f389.
using_clr_extensionsand the migrated run differ by exactly one line, the deprecation warning. The migrated output is the same 237 lines that101samples.expectedalready held before the rebase, so the file didn't change.receiver.method(name)resolves to there. All of them ran. 112 reach aSystem.Linq.Enumerablemethod, 79 through the extension methods alone and 33 merged with a CLR method group of the receiver. The other 7 areArray#joinon whatto_areturned, which is meant to be Ruby's.101samples.rbkeeps its CRLF line endings.using_clr_extensionstoo, and the two forms still differ only by the warning.ClrExtensionMethods0–8andClrScopedExtensions1–5pass.Not run: net8.0, because this machine has no .NET 8 runtime, and Windows.
Found, not fixed
IronRuby calls public methods of LINQ's private classes. On .NET 10:
range(1, 3).Take(0).to_aran for two minutes before I stopped it. It also decides overloads for blocks:digits.where { |digit, index| ... }passes nil forindex, whereSystem::Linq::Enumerable.Where(digits) { |digit, index| ... }and the same block on aListget the indexed overload. None of this needs ausing, ands.take(2).skip(5)inside one isniltoo. On .NET 9 and later,Enumerable.SkipandTakecheck their argument and then callvirtualmethods of the privateEnumerable.Iterator<T>, which may return null for an empty result or assume a count the public method has already checked.RubyClass.IsVisible(Src/Ruby/Builtins/RubyClass.cs:1342) accepts every public method whatever its declaring type, although its comment says a public method of an internal type is visible only if it implements an interface method. Sos.Skip(5)findsIterator<Object>.Skip, and the instance method wins the tie withEnumerable.Skip. The fix isn't small. Hiding the public members of non-public types would also hide the properties of C# anonymous types, which are internal classes. The sample doesn't hit it, because its counts are positive and in range.Minwith a projection is ambiguous.words.min(System::Func[Object, Integer].new { |w| w.length })raisesAmbiguousMatchExceptionunder both forms. The Func fits bothMin<TSource>(IEnumerable<TSource>, Func<TSource, int>)andMin<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>). C# picks the first becauseintis more specific thanTResult, and IronRuby's overload resolution has no such rule. The sample worked around it before this PR and still does.Stack
using_clr_extensions, now upstream as CLR extension methods reach a method group used before using_clr_extensions Largo/ironruby#43using System::Linq: CLR extension methods scoped like C#'s using directive #3 scopes CLR extension methods withusingusing System::Linq,to_seqas a refinement, blocks, and CI runs them #5, this PR, stacked onusing System::Linq: CLR extension methods scoped like C#'s using directive #3, moves101samples.rbtousing. It's the follow-up thatusing System::Linq: CLR extension methods scoped like C#'s using directive #3's description mentions.🤖 Generated with Claude Code