Skip to content

CLR interop gaps: super into CLR constructors, CLR constants, strings for Object parameters - #31

Merged
Largo merged 3 commits into
feature/clr-interop-modernfrom
feature/clr-interop-gaps
Oct 27, 2026
Merged

Largo merged 3 commits into
feature/clr-interop-modernfrom
feature/clr-interop-gaps

Conversation

@Largo

@Largo Largo commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Summary

Follow-up to #26 (and stacked on it): it closes the gaps #26 left in calling .NET from Ruby.

super in initialize runs the CLR base constructor

A Ruby class derived from a CLR class could not pass arguments to the base constructor.

  • super(name) in initialize reached Object#initialize and failed with "wrong number of arguments (given 1, expected 0)".
  • A base class without a parameterless constructor could not be instantiated at all ("define Doctor#new singleton method instead of Doctor#initialize").
class Doctor < Human            # Human(string name) only
  def initialize(name)
    super("Dr. #{name}")
    @title = "MD"
  end
end

Now super in initialize runs the matching CLR base constructor on the new instance, as base(...) does in a C# constructor. This works with:

  • keywords (super(**opts) → object initializer)
  • zsuper
  • splats
  • protected constructors of abstract classes
  • several Ruby levels

Allocation:

  • If the base has a parameterless constructor, allocation still uses it, so an initialize without super works exactly as before (class MyForm < Form). A later super() does nothing more.
  • If it has none, new allocates the instance unconstructed: its Ruby class is set, so virtual calls from the base constructor reach Ruby overrides. If initialize never calls super, which C# would not compile, new raises a TypeError saying so.

CLR constants

CLR const and static readonly fields are now constants of their class, as Java's static final fields are in JRuby. Examples: System::Math::PI, String::Empty, enum values (Color::Green).

Properties and other static members stay methods, as they always were. A Ruby constant is cached where it is used, which DateTime.Now must not be. Naming one as a constant now says how to call it:

uninitialized constant System::Threading::Tasks::Task::Factory (Factory is a static property of the CLR type, called as a method: System::Threading::Tasks::Task.Factory)

Other details:

  • Builtin classes keep MRI's constants: Integer::MaxValue is still a NameError, and System::Int32.MaxValue still works.
  • :: on a type group qualifies as its non-generic member: System::Nullable::GetUnderlyingType(...).

Ruby strings for Object parameters

A Ruby String passed for a CLR method's Object parameter now arrives as a System.String instead of IronRuby's MutableString. CLR code that inspects what it gets didn't understand MutableString:

  • ADO.NET's AddWithValue raised "No mapping exists from object type MutableString".
  • System.Text.Json serialized the string's internals.

JRuby passes a java.lang.String the same way. Ruby library methods are unaffected.

Behaviour change to be aware of: a string stored through an Object parameter, e.g. ArrayList#Add or Dictionary[Object, Object]#[]=, comes back as a System::String.

  • That value compares, hashes and matches in case/when like the Ruby string it was, and it answers Ruby String methods.
  • Only String === and #inspect's quotes tell the two apart.
  • One spec in the original interop suite (Tests/Interop/net/bcl/idictionary) pinned the old inspect output ({:foo=>"bar"}, now {:foo=>'bar'}).

Testing

  • IronRuby.Tests: 0 failures. Four new tests: ClrSuperConstructor1/2 in the new ClrSubclassingTests.cs, plus ClrConstantFields1 and ClrStringObjectArguments1. ClrConstructor3 expected the old allocation error and now gets the new one, after the module's initialize has run.
  • spec/language: 2934 examples, 0 failures, 0 errors.
  • spec/core: identical to CLR interop for modern Ruby: keywords, blocks and delegates, pattern matching, on-demand BCL loading #26 in every directory: 23,136 examples, 0 failures, 20 errors (the known ObjectSpace.each_object ones). spec/library: 6,398 examples, 0 failures, 0 errors.
  • The original interop suite (Tests/Interop/net):
    • The "properly selects overloaded constructors for super" spec now passes.
    • The only new failure is the idictionary inspect expectation described above.
  • WPF: not exercised; this was all on Linux.

🤖 Generated with Claude Code

Largo and others added 3 commits October 27, 2026 14:13
… Object

Three things that did not work when calling .NET from Ruby:

- A Ruby class derived from a CLR class could not pass arguments to the
  base class's constructor: super in initialize reached Object#initialize
  ("wrong number of arguments (given 1, expected 0)"), and a base class
  without a parameterless constructor could not be allocated at all
  ("define Doctor#new singleton method instead of Doctor#initialize").
  super in initialize now runs the matching CLR base constructor on the new
  instance, as base(...) does in a C# constructor - with keywords, splats,
  protected constructors of abstract classes, through several Ruby levels.
  A base class with a parameterless constructor is still constructed with
  it on allocation (so an initialize without super works as before; super()
  then does nothing more). Without one, new allocates the instance
  unconstructed - its Ruby class set, so virtual calls from the base
  constructor reach Ruby overrides - and raises if initialize never calls
  super, which C# would not have compiled.
- CLR const and static readonly fields are constants of their class:
  System::Math::PI, String::Empty, an enum's values, as Java's static final
  fields are in JRuby. Properties and other static members stay methods -
  a constant is cached where it is used, which DateTime.Now must not be -
  and naming one as a constant (Task::Factory) says how to call it
  (Task.Factory). Builtin classes such as Integer keep MRI's constants.
  `::` on a type group (System::Nullable::GetUnderlyingType) qualifies as
  its non-generic member, as a type group does where a module is expected.
- A Ruby String passed for a CLR method's Object parameter arrives as a
  System.String instead of a MutableString, which CLR code that looks at
  what it gets did not understand: ADO.NET's AddWithValue raised "No
  mapping exists from object type MutableString", System.Text.Json
  serialized its internals. JRuby passes a java.lang.String likewise. Ruby
  library methods are unaffected. A string stored through an Object
  parameter (ArrayList#Add) comes back as a System::String, which compares,
  hashes and matches in case/when like the Ruby string it was and answers
  Ruby String methods; only String === and #inspect's quotes tell them
  apart.

ClrConstructor3 expected the old allocation error; it now gets the new one,
after the module's initialize has run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Ruby subclass of Proc (or Array, Hash, String) whose initialize calls
super reached the new CLR-constructor path, because those builtins are CLR
types too, and Proc's constructors do not take a block's arguments:
spec/core/proc/new_spec "called on a subclass of Proc" raised ArgumentError.
super into a CLR constructor, and allocating unconstructed, are now only for
plain CLR classes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fced1b70-b4aa-46a5-baff-bbece0926d25

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Largo
Largo merged commit 27203bc into feature/clr-interop-modern Oct 27, 2026
7 checks passed
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