Skip to content

Commit 7f22809

Browse files
transclaude
andcommitted
Review Binding methods
- Fix Binding#[] and #[]= to use local_variable_get/local_variable_set (broken since Ruby 1.9; now works again) - Deprecate Binding#self (use Binding#receiver, Ruby 2.6+) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e25d5f8 commit 7f22809

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

HISTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ Changes:
103103
* Redefine `String#lchomp` / `#lchomp!` as aliases for `delete_prefix` / `delete_prefix!`.
104104
* Rename `Time#trunc` to `Time#floor_to` (parallels `Time#round_to`; avoids confusion
105105
with Ruby's `Time#floor` which takes sub-second digit precision). `trunc` deprecated.
106+
* Fix `Binding#[]` and `#[]=` to use `local_variable_get`/`local_variable_set`
107+
(broken since Ruby 1.9; now works again).
108+
* Deprecate `Binding#self` (use `Binding#receiver`, adopted by Ruby in 2.6).
106109
* Deprecate `File.null` (use `File::NULL` constant, Ruby 1.9.3+).
107110
* Deprecate `File.read_binary` (use `File.binread`, Ruby 1.9.3+).
108111
* Fix dead requires for removed `kernel/singleton_class` in Proc and Kernel.

lib/core/facets/binding/op_get.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
class Binding
22

3-
# Returns the value of some variable.
3+
# Returns the value of a local variable.
44
#
55
# a = 2
6-
# binding["a"] #=> 2
6+
# binding[:a] #=> 2
77
#
8-
def []( x )
9-
eval( x.to_s )
8+
def [](name)
9+
local_variable_get(name.to_sym)
1010
end
1111

1212
# Set the value of a local variable.
1313
#
14-
# binding["a"] = 4
14+
# binding[:a] = 4
1515
# a #=> 4
1616
#
17-
# @deprecated No longer wortks in Ruby 1.9+.
18-
#
19-
# @see Binding#with for an alternative.
20-
def []=( l, v )
21-
eval( "lambda {|v| #{l} = v}").call( v )
17+
def []=(name, value)
18+
local_variable_set(name.to_sym, value)
2219
end
2320

2421
end
25-

lib/core/facets/binding/self.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
class Binding
22

3-
# This is already defined by Rubinius:
4-
# Kernel.eval('Rubinius::VariableScope.current.self', self)
5-
unless method_defined?(:self)
6-
7-
# Returns self of the binding's context.
8-
def self
9-
eval('self')
10-
end
11-
3+
# @deprecated Use Binding#receiver instead (built-in since Ruby 2.6).
4+
def self
5+
warn "Binding#self is deprecated. Use Binding#receiver instead.", uplevel: 1
6+
receiver
127
end
138

149
end
15-

0 commit comments

Comments
 (0)