Skip to content

Commit 45b1d28

Browse files
authored
Merge pull request #172 from ruby-syntax-tree/symbol-name-refinement
Use a refinement for Symbol#name instead of a polyfill
2 parents 974cdcb + 3605b50 commit 45b1d28

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ AllCops:
1313
Layout/LineLength:
1414
Max: 80
1515

16+
Lint/AmbiguousBlockAssociation:
17+
Enabled: false
18+
1619
Lint/DuplicateBranch:
1720
Enabled: false
1821

lib/syntax_tree.rb

-10
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@
2222

2323
require_relative "syntax_tree/parser"
2424

25-
# We rely on Symbol#name being available, which is only available in Ruby 3.0+.
26-
# In case we're running on an older Ruby version, we polyfill it here.
27-
unless :+.respond_to?(:name)
28-
class Symbol # rubocop:disable Style/Documentation
29-
def name
30-
to_s.freeze
31-
end
32-
end
33-
end
34-
3525
# Syntax Tree is a suite of tools built on top of the internal CRuby parser. It
3626
# provides the ability to generate a syntax tree from source, as well as the
3727
# tools necessary to inspect and manipulate that syntax tree. It can be used to

lib/syntax_tree/node.rb

+14
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,20 @@ def format(q)
16511651
# array << value
16521652
#
16531653
class Binary < Node
1654+
# Since Binary's operator is a symbol, it's better to use the `name` method
1655+
# than to allocate a new string every time. This is a tiny performance
1656+
# optimization, but enough that it shows up in the profiler. Adding this in
1657+
# for older Ruby versions.
1658+
unless :+.respond_to?(:name)
1659+
using Module.new {
1660+
refine Symbol do
1661+
def name
1662+
to_s.freeze
1663+
end
1664+
end
1665+
}
1666+
end
1667+
16541668
# [untyped] the left-hand side of the expression
16551669
attr_reader :left
16561670

0 commit comments

Comments
 (0)