[前][次][番号順一覧][スレッド一覧]

ruby-changes:70091

From: Marc-Andr=C3=A9 <ko1@a...>
Date: Tue, 7 Dec 2021 13:22:19 +0900 (JST)
Subject: [ruby-changes:70091] 95d9bcf2b2 (master): [ruby/ostruct] Alias less methods

https://git.ruby-lang.org/ruby.git/commit/?id=95d9bcf2b2

From 95d9bcf2b2921baee5dfacdf9dfd606a4c5167a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= <github@m...>
Date: Mon, 6 Dec 2021 23:05:53 -0500
Subject: [ruby/ostruct] Alias less methods

Skips methods that do not end with letter (in particular `!~` and `=~`)
For JRuby, also skip `instance_exec`, `instance_eval` and `eval`
---
 lib/ostruct.rb               | 7 ++++++-
 test/ostruct/test_ostruct.rb | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index c2de7a493d8..e320e6db41b 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -453,7 +453,12 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L453
   end
 
   # Make all public methods (builtin or our own) accessible with <code>!</code>:
-  instance_methods.each do |method|
+  give_access = instance_methods
+  # See https://github.com/ruby/ostruct/issues/30
+  give_access -= %i[instance_exec instance_eval eval] if RUBY_ENGINE == 'jruby'
+  give_access.each do |method|
+    next if method.match(/\W$/)
+
     new_name = "#{method}!"
     alias_method new_name, method
   end
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 4ec4d43084e..6487cc831c4 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -280,6 +280,7 @@ class TC_OpenStruct < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L280
     os = OpenStruct.new(method: :foo, hash: 42)
     assert_equal(os.object_id, os.method!(:object_id).call)
     assert_not_equal(42, os.hash!)
+    refute os.methods.include?(:"!~!")
   end
 
   def test_override_subclass
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]