summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc-AndrĂ© Lafortune <[email protected]>2021-12-06 23:05:53 -0500
committerMarc-AndrĂ© Lafortune <[email protected]>2021-12-06 23:21:07 -0500
commit95d9bcf2b2921baee5dfacdf9dfd606a4c5167a8 (patch)
tree18ea00a86476c9ebf1590e76863a0960b3454e20
parentdfd9728c87640d9699b1e7c63a6fefb6b2797fbc (diff)
[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`
-rw-r--r--lib/ostruct.rb7
-rw-r--r--test/ostruct/test_ostruct.rb1
2 files changed, 7 insertions, 1 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index c2de7a493d..e320e6db41 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -453,7 +453,12 @@ class OpenStruct
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 4ec4d43084..6487cc831c 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -280,6 +280,7 @@ class TC_OpenStruct < Test::Unit::TestCase
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