From 276a9f7f1b3033549429647da61eb02c49e0b67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Mon, 6 Dec 2021 23:05:53 -0500 Subject: [PATCH] Alias less methods [Fix #30] 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 c2de7a4..e320e6d 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -453,7 +453,12 @@ def init_with(coder) # :nodoc: end # Make all public methods (builtin or our own) accessible with !: - 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 4ec4d43..6487cc8 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -280,6 +280,7 @@ def test_access_original_methods 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