The code I'm trying to improve modifies a URL according to the options passed in a hash:{  :image_aspect_ratio => square,  :image_size => 50}or{  :image_size => {    :width => 50,    :height => 60  }}The code looks like this:module X  module Y    class Zdef image_url  if raw_info['picture'] && image_size_opts_passed?    image_url_with_size_opts  else    raw_info['picture']  endenddef image_size_opts_passed?  !!(options[:image_size] || options[:image_aspect_ratio])enddef image_url_with_size_opts  params_index = raw_info['picture'].index('/photo.jpg')  if params_index    raw_info['picture'].insert(params_index, image_params)  else    raw_info['picture']  endenddef image_params  image_params = []  if options[:image_size].is_a?(Integer)    image_params << s#{options[:image_size]}  elsif options[:image_size].is_a?(Hash)    image_params << w#{options[:image_size][:width]} if options[:image_size][:width]    image_params << h#{options[:image_size][:height]} if options[:image_size][:height]  end  image_params << 'c' if options[:image_aspect_ratio] == 'square'  '/' + image_params.join('-')endHere's the stuff I hate about this code:Lots of raw_info['picture'] being called, but I'm not sure if using a local variable is better than accessing a hash twice.I'm seeing some duplication in the else branch in the image_url and image_url_with_size_opts methods but I don't know how to improve that.This code is all inside a class which is inside a module which is inside another module, so I'm not sure if I could also memoize the image_params result.
Modifying an image URL based on hash options
optimization;ruby;image