Skip to content

Make ls command work for BasicObjects#1177

Merged
tompng merged 4 commits into
ruby:masterfrom
eikes:fix-ls-for-basic-objects
Mar 3, 2026
Merged

Make ls command work for BasicObjects#1177
tompng merged 4 commits into
ruby:masterfrom
eikes:fix-ls-for-basic-objects

Conversation

@eikes

@eikes eikes commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

When working with the builder gem I noticed that the irb inspection of the Builder::XmlMarkup instances was not working as expected. Upon further investigation it turns out, that the reason is that this class inherits from BasicObject directly and not from the more common Object.

You can see the problem here:

4.0.1 :001 > ls BasicObject.new
/gems/irb-1.17.0/lib/irb/command/ls.rb:58:in 'IRB::Command::Ls#execute': undefined method 'class' for an instance of BasicObject (NoMethodError)

        klass  = (obj.class == Class || obj.class == Module ? obj : obj.class)
                     ^^^^^^

I have also created a new ls command test file containing a failing test which you can see below: https://github.com/ruby/irb/actions/runs/22390651625/job/64811518726

The test is fixed with the second commit of this PR by using a slightly different way to inspect the class. singleton_class and instance_variables of an object.

PS: The prism-latest test failure is because the syntax of ruby 4.1 allows trailing commas in method definitions and this change is addressed here: #1178

Comment thread lib/irb/command/ls.rb Outdated

klass = (obj.class == Class || obj.class == Module ? obj : obj.class)
klass = Kernel.instance_method(:class).bind(obj).call
is_class_or_module = klass == Class || klass == Module

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can just do Kernel.instance_method(:is_a?).bind(obj).call(Module)? If an object is a Class, it's definitely a Module as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think about that, but it's true! I just kept it as it was but I'll change it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessarily easier to read though:

-        is_class_or_module = klass == Class || klass == Module
+        is_class_or_module = Kernel.instance_method(:is_a?).bind(obj).call(Module)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about Module === obj

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's neat! I changed it 👍

@eikes
eikes force-pushed the fix-ls-for-basic-objects branch from bc0baa1 to 301bf3f Compare February 25, 2026 17:05
@eikes

eikes commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

Just rebased it on master to make the prism test pass...

@eikes

eikes commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

@tompng @st0012 anything else you'd like to see so you can merge this PR?

@tompng tompng left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 👍

@tompng
tompng merged commit 384f338 into ruby:master Mar 3, 2026
40 checks passed
@st0012 st0012 added the bug Something isn't working label Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants