Skip to content

Commit b76c482

Browse files
committed
Alias less methods [Fix ruby#30]
Skips methods that do not end with letter (in particular `!~` and `=~`) For JRuby, also skip `instance_exec`, `instance_eval` and `eval`
1 parent a901df2 commit b76c482

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/ostruct.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,12 @@ def init_with(coder) # :nodoc:
453453
end
454454

455455
# Make all public methods (builtin or our own) accessible with <code>!</code>:
456-
instance_methods.each do |method|
456+
give_access = instance_methods
457+
# See https://github.com/ruby/ostruct/issues/30
458+
give_access -= %i[instance_exec instance_eval eval] if RUBY_ENGINE == 'jruby'
459+
give_access.each do |method|
460+
next if method.match(/\W$/)
461+
457462
new_name = "#{method}!"
458463
alias_method new_name, method
459464
end

test/ostruct/test_ostruct.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ def test_access_original_methods
280280
os = OpenStruct.new(method: :foo, hash: 42)
281281
assert_equal(os.object_id, os.method!(:object_id).call)
282282
assert_not_equal(42, os.hash!)
283+
refute os.methods.include?(:"!~!")
283284
end
284285

285286
def test_override_subclass

0 commit comments

Comments
 (0)