How about:
def toggle_attribute
statement1
@response = yield(
param1: param1value,
param2: param2value
)
statement2
end
def disable_attribute
toggle_attribute { |params| Client::Service.disable_attribute(params) }
end
def enable_attribute
toggle_attribute { |params| Client::Service.enable_attribute(params) }
end
If you want to be more succinct, you could:
def toggle_attribute(method_name)
statement1
@response = Client::Service.send(method_name,
param1: param1value,
param2: param2value
)
statement2
end
def disable_attribute
toggle_attribute(:disable_attribute)
end
def enable_attribute
toggle_attribute(:enable_attribute)
end
And if you want to really impress your friends, you could write:
def toggle_attribute(method_name)
statement1
@response = Client::Service.send(method_name,
param1: param1value,
param2: param2value
)
statement2
end
def method_missing(method_name, *args)
%i[disable_attribute enable_attribute].include?(method_name) ?
toggle_attribute(method_name) : super
end