about summary refs log tree commit homepage
path: root/lib/dtas.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2017-03-02 04:08:23 +0000
committerEric Wong <e@80x24.org>2017-03-02 04:08:43 +0000
commitf4c2ac4825f99ec4af529d65ae98abfaf034fb0f (patch)
treebbae58219dc4d158c033b892e0a7da67a969ecdc /lib/dtas.rb
parentc2e04a5715d4cfa2b42fa09ba1f641d3ce741685 (diff)
downloaddtas-f4c2ac4825f99ec4af529d65ae98abfaf034fb0f.tar.gz
This is faster than relying on eval() for older Rubies.
https://bugs.ruby-lang.org/issues/13077
Ruby 2.5 is targetted for release in December 2017.
Diffstat (limited to 'lib/dtas.rb')
-rw-r--r--lib/dtas.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/dtas.rb b/lib/dtas.rb
index 1a1a882..ac416d7 100644
--- a/lib/dtas.rb
+++ b/lib/dtas.rb
@@ -24,6 +24,19 @@ module DTAS
   def self.null # :nodoc:
     @null ||= File.open('/dev/null', 'r+')
   end
+
+  # String#-@ will deduplicate strings when Ruby 2.5 is released (Dec 2017)
+  # https://bugs.ruby-lang.org/issues/13077
+  if RUBY_VERSION.to_f >= 2.5
+    def self.dedupe_str(str)
+      -str
+    end
+  else
+    # Ruby 2.1 - 2.4, noop for older Rubies
+    def self.dedupe_str(str)
+      eval "#{str.inspect}.freeze"
+    end
+  end
   # :startdoc:
 end