Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to explicit method call in completion #369

Merged
merged 5 commits into from
Oct 2, 2022

Conversation

osyo-manga
Copy link
Contributor

Ensure that methods are called even when local variables are defined.
see: #368

Comment on lines 299 to 300
gv = eval("send(:global_variables)", bind).collect{|m| m.to_s}.push("true", "false", "nil")
lv = eval("send(:local_variables)", bind).collect{|m| m.to_s}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Under Ruby 2.6, private methods cannot be called with self., so #send is used.

@osyo-manga osyo-manga marked this pull request as ready for review May 30, 2022 14:12
@osyo-manga osyo-manga changed the title Change to explicit method call Change to explicit method call in completion May 30, 2022
Ensure that methods are called even when local variables are defined.
see: ruby#368
@ksss
Copy link

ksss commented Jun 9, 2022

Thanks for the support.
Would it be possible to support if the methods method is overridden?

class Foo
  def methods
    binding.irb
    1
  end
end

Foo.new.methods

If we can use UnboundMethod#bind to call the method before the overwrite, it may be possible to handle this, but I don't know if it can be done...

@osyo-manga
Copy link
Contributor Author

Thanks for comment.
Supported b01b38d.

test/irb/test_completion.rb Outdated Show resolved Hide resolved
str_example = ''
str_example.clear # suppress "assigned but unused variable" warning
@str_example = ''
Copy link
Member

Choose a reason for hiding this comment

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

It looks like str_example and @str_example are test subjects, and the rest are for making sure irb won't confuse them when completion?

If that's the case, can we add comments to mention that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks comment.
I added comment 697708d.

using Module.new {
refine ::Binding do
def eval_methods
eval("::Kernel.instance_method(:methods).bind(self).call")
Copy link
Member

@st0012 st0012 Sep 16, 2022

Choose a reason for hiding this comment

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

Instead of wrapping the entire chain inside eval, can we only wrap the necessary part?

  def eval_methods
    ::Kernel.instance_method(:methods).bind(eval("self")).call
  end

  def eval_private_methods
    ::Kernel.instance_method(:private_methods).bind(eval("self")).call
  end

  def eval_instance_variables
    ::Kernel.instance_method(:instance_variables).bind(eval("self")).call
  end

  def eval_global_variables
    ::Kernel.instance_method(:global_variables).bind(eval("self")).call
  end

  def eval_class_constants
    ::Module.instance_method(:constants).bind(eval("self.class")).call
  end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks comments :)
I fixed it.
78ef990

@st0012
Copy link
Member

st0012 commented Sep 16, 2022

@osyo-manga I think the PR is almost ready. Can you do the small adjustments I mentioned above? Thx

@st0012
Copy link
Member

st0012 commented Sep 30, 2022

@osyo-manga Given that this issue crashes irb, I want to make sure it's fixed and tested before Ruby 3.2. So I opened #404 based on your commits. Let me know if you prefer making those changes yourself, I'll close mine 🙂

@osyo-manga
Copy link
Contributor Author

@st0012 Sorry for the delay in replying.
PR has been updated.

@st0012
Copy link
Member

st0012 commented Oct 1, 2022

@osyo-manga Thank you 👍
@peterzhu2118 Can you help me merge this? Thx

Copy link
Member

@peterzhu2118 peterzhu2118 left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution!

@peterzhu2118 peterzhu2118 merged commit c34d54b into ruby:master Oct 2, 2022
matzbot pushed a commit to ruby/ruby that referenced this pull request Oct 2, 2022
Ensure that methods are called even when local variables are defined.
see: ruby/irb#368

ruby/irb@c34d54b8bb
@osyo-manga
Copy link
Contributor Author

Thank for merge :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants