ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "mame (Yusuke Endoh) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "mame (Yusuke Endoh)" <noreply@ruby-lang.org>
Subject: [ruby-core:120743] [Ruby master Bug#21049] Reconsider handling of the numbered parameters and "it" parameter in `Binding#local_variables`
Date: Mon, 20 Jan 2025 09:46:09 +0000 (UTC)	[thread overview]
Message-ID: <redmine.issue-21049.20250120094608.18@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-21049.20250120094608.18@ruby-lang.org>

Issue #21049 has been reported by mame (Yusuke Endoh).

----------------------------------------
Bug #21049: Reconsider handling of the numbered parameters and "it" parameter in `Binding#local_variables`
https://bugs.ruby-lang.org/issues/21049

* Author: mame (Yusuke Endoh)
* Status: Open
* Assignee: matz (Yukihiro Matsumoto)
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
Currently, `Binding#local_variable*` APIs wrongly handles the numbered parameters.

```ruby
# Binding#local_variables includes numbered parameters that should not be visible
"foo".tap do
  _1
  "bar".tap do
    p binding.local_variables #=> expected: [], actual: [:_1]
  end
end

# Binding#local_variable_get can read numbered parameters that should not be readable
"foo".tap do
  _1
  "bar".tap do
    p binding.local_variable_get(:_1) #=> expected: NameError, actual: "foo"
  end
end

# Binding#local_variable_set can update numbered parameter
"foo".tap do
  p _1 #=> "foo"
  binding.local_variable_set(:_1, "bar") # expected: NameError, actual: updates _1
  p _1 #=> "bar"
end
```

My proposal is to stop handling numbered parameters and "it" parameter in `Binding#local_variable*` APIs.

```ruby
"foo".tap do
  _1
  binding.local_variables         #=> proposed: []
  binding.local_variable_get(:_1) #=> proposed: NameError
end
```

Here is a proof-of-concept patch.

https://github.com/ruby/ruby/pull/12601

It would be theoretically possible to fix `Binding#local_variable*` APIs to handle numbered parameters while maintaining compatibility as much as possible.
However, I think the implicit “it” parameter introduced in Ruby 3.4 poses a spec-level problem.
This is because there is no way to distinguish between the implicit “it” parameter and a true local variable named "it".

Also, I don't think numbered parameters are local variables, so I feel uncomfortable handling them by `Binding#local_variable*` APIs.

If it is absolutely necessary to access numbered parameters or "it" parameter via binding, it would be good to introduce dedicated APIs for the purpose, as:

* Binding#numbered_parameters              #=> [:_1, :_2, :_3, ...]
* Binding#numbered_parameter_get(:_1)      #=> obj
* Binding#numbered_parameter_defined?(:_1) #=> true or false
* Binding#it_get       #=> obj
* Binding#it_defined?  #=> true or false

Personally, however, I do not see the need for these APIs currently. I implemented them as proof-of-concept in PR, but I am okay that they are not introduced.




-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

       reply	other threads:[~2025-01-20  9:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-20  9:46 mame (Yusuke Endoh) via ruby-core [this message]
2025-02-13  5:58 ` [ruby-core:120965] " matz (Yukihiro Matsumoto) via ruby-core
2025-02-18  8:51 ` [ruby-core:121099] " mame (Yusuke Endoh) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.issue-21049.20250120094608.18@ruby-lang.org \
    --to=ruby-core@ml.ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).