ruby on rails - Refactor case/when using strings -
i have number of these in controller:
def ups @ups ||= shipper::ups.new( env['ups_access_key'], env['ups_password'], env['ups_username'], env['ups_account'] ) end
and have block gets called:
def type(number, carrier) case carrier.slug when 'ups' number_details = ups.track(number) when 'fedex' number_details = fedex.track(number) when 'usps' number_details = usps.track(number) end return number_details end
but seems refactor quite bit if take carrier.slug
, prepend lines ups.track(number)
.
is there way that?
you can use send
before need make sure have right carrier slug
if %w[ups fedex usps].include?(carrier.slug) send(carrier.slug).track(number) end
Comments
Post a Comment