[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`
This commit is contained in:
Marc-André Lafortune 2021-12-06 23:05:53 -05:00
Родитель dfd9728c87
Коммит 95d9bcf2b2
2 изменённых файлов: 7 добавлений и 1 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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