[ruby-core:96351] [Ruby master Feature#16432] Using `_1` inside `binding.irb` will cause unintended behavior

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-83262.20191220010509.9bf60330944812c3@ruby-lang.org>
Issue #16432 has been updated by osyo (manga osyo).


Thanks comments :)

> This is because the eval code is executed at the current context exactly.
> Another choice is to raise a syntax error at the runtime.

Yes, and if `_1` is nested, a syntax error will occur.

```ruby
proc {
  _1
  # error: numbered parameter is already used in
  (1..10).map { _1 + _1 }
}.call 42
```

If `_1` is nested, I think it is better to runtime error rather than unintended behavior.

```ruby
# Expected behavior in case of runtime error
# OK
irb> _1
=> 42
# OK
irb> _1 + _1
=> 84
# NG: Runtime error
irb> (1..10).map { _1 + _1 }
```


----------------------------------------
Feature #16432: Using `_1` inside `binding.irb` will cause unintended behavior
https://bugs.ruby-lang.org/issues/16432#change-83262

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## Summary

Calling `binding.irb` in a block that uses `_1` and using `_1` in `irb` will cause unintended behavior.


## Steps to reproduce

1. Use `_1` in block
2. Use `binding.irb` in block
3. Use block with `_1` in `binding.irb`

```ruby
# test.rb
proc {
  binding.irb
  _1
}.call 42
```

```ruby
# binding.irb
irb> (1..10).map { _1 + _1 }
```


## Expected behavior

```ruby
irb> _1
 => 42
irb> (1..10).map { _1 + _1 }
 = > [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```


## Actual behavior

* Refers to the `_1` of the block that called` binding.irb`

```ruby
irb> _1
 => 42
irb> (1..10).map { _1 + _1 }
 = > [84, 84, 84, 84, 84, 84, 84, 84, 84, 84]
```

I think this is an unintended behavior for the user.






-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.