Skip to content

Commit

Permalink
ZsockOptions: avoid calling #public_methods in #[] and #[]= if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
paddor committed Jul 8, 2024
1 parent 7eed2dd commit bdca8e2
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/cztop/zsock_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,41 @@ def initialize(zocket)

# Fuzzy option getter. This is to make it easier when porting
# applications from CZMQ libraries to CZTop.
#
# @param option_name [Symbol, String] case insensitive option name
# @raise [NoMethodError] if option name can't be recognized
def [](option_name)
# NOTE: beware of predicates, especially #CURVE_server? & friends
meth = public_methods.grep_v(/=$/)
.find { |m| m =~ /^#{option_name}\??$/i }
raise NoMethodError, option_name if meth.nil?
meth1 = :"#{option_name}"
meth2 = :"#{option_name}?"

if respond_to? meth1
meth = meth1
elsif respond_to? meth2
meth = meth2
else
# NOTE: beware of predicates, especially #CURVE_server? & friends
meth = public_methods.grep_v(/=$/)
.find { |m| m =~ /^#{option_name}\??$/i }
raise NoMethodError, option_name if meth.nil?
end

__send__(meth)
end


# Fuzzy option setter. This is to make it easier when porting
# applications from CZMQ libraries to CZTop.
#
# @param option_name [Symbol, String] case insensitive option name
# @param new_value [String, Integer] new value
# @raise [NoMethodError] if option name can't be recognized
def []=(option_name, new_value)
meth = public_methods.find { |m| m =~ /^#{option_name}=$/i }
raise NoMethodError, option_name if meth.nil?
meth = :"#{option_name}="

unless respond_to? meth
meth = public_methods.find { |m| m =~ /^#{option_name}=$/i }
raise NoMethodError, option_name if meth.nil?
end

__send__(meth, new_value)
end
Expand Down

0 comments on commit bdca8e2

Please sign in to comment.