Skip to content

Helper methods without object polluting#1216

Draft
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:non_polluting_helper_method
Draft

Helper methods without object polluting#1216
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:non_polluting_helper_method

Conversation

@tompng
Copy link
Copy Markdown
Member

@tompng tompng commented May 17, 2026

Instead of defining helper method to current workspace object, modify the input code to directly call helper method defined in a container object.
p conf.ap_name will be modified to p ::IRB::HelperMethod::Container.conf.ap_name.

Problem

Main objects are polluted by helper methods.

my_rails_app(dev)> user = User.first
my_rails_app(dev)> cd user
my_rails_app(dev)> cd
my_rails_app(dev)> user.controller
=> #<ApplicationController:0x0000000028cbd0>

Especially when using with step debug command, many objects were polluted.

binding.irb
objects.sample(4).each { it.foo } # step into each object
p objects.count { _1.respond_to? :conf } #=> 4

Polluted object may break application/library code that has respond_to? checks.
These codes were found in GitHub code search: respond_to?(:conf) respond_to?(:helper) respond_to?(:app) respond_to?(:controller) respond_to?(:reload!) respond_to?(:new_session).

Helper methods were method call to a polluted object, dynamically determined. We can't autocomplete or show a document dialog for helper methods

Changes

Before: Calling conf on polluted object is a helper method call, dynamically determined.
After: conf without receiver is always helper method call. conf with receiver, statically determined.

Statements Before After
obj.instance_eval { conf } obj.conf helper method
tap(&:conf) helper method self.conf
instance_eval { send(:conf) } helper method self.send(:conf)
eval("conf") helper method eval("conf")
self.conf helper method self.conf
self.tap { it.conf } helper method self.conf
class A; conf; end A.conf helper method

Helper method calls are colored BOLD just like commands. I think this syntax highlighting is required to avoid confusion.
image
Also implements completion and document dialog for helper method.

Pros/Cons

Pros

No polluting

Cons

conf and other helper methods such as controller app, reload! without receiver will be always treated as helper method, even if self have the same method.

irb> Data.define(:conf).new(42).instance_eval { conf }.class
#=> IRB::Context, not Integer

irb> def conf = 42
irb> [conf.class, self.conf.class]
# => [IRB::Context, Integer]

Another options

  • Make helper methods a method defined on TOP_LEVEL main
  • Make helper methods a method defined on Kernel

@tompng tompng marked this pull request as draft May 18, 2026 05:38
@tompng tompng force-pushed the non_polluting_helper_method branch 2 times, most recently from eb3d4c0 to 827cbc2 Compare May 18, 2026 15:37
Instead of defining helper method to current workspace object, modify the input code to directly call helper method defined in a container object.
`p conf.ap_name` will be modified to `p ::IRB::HelperMethod::Container.conf.ap_name`.
Also implements highlighting, completion, and document dialog for helper methods.
@tompng tompng force-pushed the non_polluting_helper_method branch from 827cbc2 to 687e677 Compare May 18, 2026 16:56
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