Skip to content

101 LINQ samples: using System::Linq, to_seq as a refinement, blocks, and CI runs them - #5

Merged
sholden merged 4 commits into
feature/scoped-clr-extensionsfrom
fix/linq-samples-scoped-using
Oct 29, 2026
Merged

sholden merged 4 commits into
feature/scoped-clr-extensionsfrom
fix/linq-samples-scoped-using

Conversation

@sholden

@sholden sholden commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Src/Samples/Linq/101samples.rb, IronRuby's port of Microsoft's 101 LINQ Samples, was the last code outside the tests that called using_clr_extensions, so since #3 it printed the deprecation warning. It says using System::Linq now. 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 to using.

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, because Fixnum has 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|. Fixnum is Integer, which is System::Int32 in IronRuby.
  • "Min: Projection" called sum, copied from "Sum: Projection", and printed 25, the total length of the words. It calls min now and prints 5. It goes through method(:min).of(Object, Integer), as "Min: Grouped" already did, because words.min(System::Func[Object, Integer].new { ... }) raises AmbiguousMatchException under either form. See "Found, not fixed".
  • "Repeat" printed 7777777777 without a newline, so the next heading ran on.
  • "OfType" was commented out with # 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 using

Since #3's last two commits, a call inside a using System::Linq scope resolves exactly as it did under using_clr_extensions. So the second commit changes the using line and nothing else about the calls: select, first, count, group_by and the rest keep their names and still reach LINQ.

It also turns to_seq, which makes the samples' Ruby arrays into the IEnumerable<T> LINQ extends, from a method added to Object for the whole program into a refinement:

module LinqSampleHelpers
  refine Object do
    def to_seq(type = Object)
      System::Linq::Enumerable.method(:of_type).of(type).call(self.to_a)
    end
  end
end

using System::Linq
using LinqSampleHelpers

Nothing in the file calls to_seq before those lines. The two classes the file defines, Comparer and AnagramEqualityComparer, 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 are make_pair and identity.

Two indexed samples keep a lambda, "Where: Indexed" and "Select: Indexed". Their receiver comes from OfType, and on .NET 9 and later it has a Where(Func<T, bool>) and a Select(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. So digits.where { |digit, index| ... } got nil for index, and numbers.select { |num, index| ... } raised AmbiguousMatchException. A lambda's arity rules them out. The indexed select_many, take_while and skip_while have 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-test runs the sample after the socket check, on all four OS and framework legs, and diffs its stdout and stderr against the new Src/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: putting using_clr_extensions back, changing count(pred) to where(pred), adding a raise, and dropping using LinqSampleHelpers. The comment in ci.yml gives the command that regenerates the file.

Nothing outside Src/IronRuby.Tests recommends using_clr_extensions any 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.

  • The reference run under using_clr_extensions and the migrated run differ by exactly one line, the deprecation warning. The migrated output is the same 237 lines that 101samples.expected already held before the rebase, so the file didn't change.
  • A scratch probe, not committed, sat before each of the 119 calls with a LINQ method's name and recorded what receiver.method(name) resolves to there. All of them ran. 112 reach a System.Linq.Enumerable method, 79 through the extension methods alone and 33 merged with a CLR method group of the receiver. The other 7 are Array#join on what to_a returned, which is meant to be Ruby's.
  • The indexed overloads still get the index. "Where: Indexed", "Select: Indexed", "SelectMany: Indexed", "TakeWhile: Indexed" and "SkipWhile: Indexed" print what they did before, and I checked those results by hand against their predicates.
  • 101samples.rb keeps its CRLF line endings.
  • The block version runs under using_clr_extensions too, and the two forms still differ only by the warning.
  • ClrExtensionMethods0–8 and ClrScopedExtensions1–5 pass.

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:

s = System::Linq::Enumerable.method(:of_type).of(Object).call([1, 2, 3])
s.Take(2).Skip(5)                                       # => nil, C# gives an empty sequence
System::Linq::Enumerable.range(1, 3).Skip(-2).to_a      # => [-1, 0, 1, 2, 3], C# gives [1, 2, 3]
System::Linq::Enumerable.range(1, 3).Take(0).first(5)   # => [1, 2, 3, 4, 5], C# gives []

range(1, 3).Take(0).to_a ran for two minutes before I stopped it. It also decides overloads for blocks: digits.where { |digit, index| ... } passes nil for index, where System::Linq::Enumerable.Where(digits) { |digit, index| ... } and the same block on a List get the indexed overload. None of this needs a using, and s.take(2).skip(5) inside one is nil too. On .NET 9 and later, Enumerable.Skip and Take check their argument and then call virtual methods of the private Enumerable.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. So s.Skip(5) finds Iterator<Object>.Skip, and the instance method wins the tie with Enumerable.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.

Min with a projection is ambiguous. words.min(System::Func[Object, Integer].new { |w| w.length }) raises AmbiguousMatchException under both forms. The Func fits both Min<TSource>(IEnumerable<TSource>, Func<TSource, int>) and Min<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>). C# picks the first because int is more specific than TResult, and IronRuby's overload resolution has no such rule. The sample worked around it before this PR and still does.

Stack

  1. CLR extension methods reach a method group used before using_clr_extensions #1 fixes stale CLR overloads after using_clr_extensions, now upstream as CLR extension methods reach a method group used before using_clr_extensions Largo/ironruby#43
  2. Refinements: only refined calls pay, and the JIT and OSR keep working #2 makes refinements cost nothing where they can't change a call
  3. using System::Linq: CLR extension methods scoped like C#'s using directive #3 scopes CLR extension methods with using
  4. 101 LINQ samples: using System::Linq, to_seq as a refinement, blocks, and CI runs them #5, this PR, stacked on using System::Linq: CLR extension methods scoped like C#'s using directive #3, moves 101samples.rb to using. It's the follow-up that using System::Linq: CLR extension methods scoped like C#'s using directive #3's description mentions.

🤖 Generated with Claude Code

@sholden
sholden added this pull request to stack #12 October 29, 2026 11:01
@sholden
sholden force-pushed the fix/linq-samples-scoped-using branch from 3178c86 to 9195092 Compare October 29, 2026 15:19
@sholden sholden changed the title 101 LINQ samples: using System::Linq, and CI runs them 101 LINQ samples: using System::Linq, to_seq as a refinement, and CI runs them Oct 29, 2026
sholden and others added 3 commits October 29, 2026 08:32
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
sholden force-pushed the fix/linq-samples-scoped-using branch from 9195092 to 621d38b Compare October 29, 2026 15:33
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant