Skip to content

Commit de7e246

Browse files
committed
Only define singleton methods for overrides
Don't define singleton methods unless they are needed to override existing methods. Otherwise, just rely on `method_missing` and `respond_to_missing?`.
1 parent f3f0ca4 commit de7e246

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/ostruct.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def marshal_dump # :nodoc:
231231
# OpenStruct. It does this by using the metaprogramming function
232232
# define_singleton_method for both the getter method and the setter method.
233233
#
234-
def new_ostruct_member!(name) # :nodoc:
234+
def override_ostruct_method!(name) # :nodoc:
235235
unless @table.key?(name) || is_method_protected!(name)
236236
if defined?(::Ractor)
237237
getter_proc = nil.instance_eval{ Proc.new { @table[name] } }
@@ -246,11 +246,11 @@ def new_ostruct_member!(name) # :nodoc:
246246
define_singleton_method!("#{name}=", &setter_proc)
247247
end
248248
end
249-
private :new_ostruct_member!
249+
private :override_ostruct_method!
250250

251251
private def is_method_protected!(name) # :nodoc:
252252
if !respond_to?(name, true)
253-
false
253+
true
254254
elsif name.match?(/!$/)
255255
true
256256
else
@@ -340,7 +340,7 @@ def [](name)
340340
#
341341
def []=(name, value)
342342
name = name.to_sym
343-
new_ostruct_member!(name)
343+
override_ostruct_method!(name)
344344
@table[name] = value
345345
end
346346
alias_method :set_ostruct_member_value!, :[]=

0 commit comments

Comments
 (0)