Finding locations with a distance between 1 and 5000 I'm wondering how I could refactor this code. It may look a little reiterative and probably there's a shorter method to accomplish the same.

def self.location(distance=100,location)
  if distance.is_a? Integer 
    if distance.between?(1,5000)
      distance = distance
    elsif distance &lt; 1
      distance = 1
    elsif distance &gt; 5000
      distance = 5000
    end
  else 
    distance = 100
  end
  if location
    within(distance, :origin =&gt; location)
  else  
    find(:all)  
  end  
end
Shorter, and while min/max was tricky, IMO, this is easier to understand:

def self.location(distance=100,location)
    distance = 100 unless distance.is_a?(Integer)
    distance = 1 if distance &lt; 1
    distance = 5000 if distance &gt; 5000

    if location
        within(distance, :origin =&gt; location)
    else  
        find(:all)  
    end  
end