Skip to content

Instantly share code, notes, and snippets.

@psfblair
Created June 19, 2010 19:49
Show Gist options
  • Select an option

  • Save psfblair/445217 to your computer and use it in GitHub Desktop.

Select an option

Save psfblair/445217 to your computer and use it in GitHub Desktop.
def distance(*args)
flattened = args.flatten
if flattened.length == 3
hash_arg = flattened.pop
raise ArgumentError unless hash_arg.respond_to?(:each_key)
flattened << hash_arg[:to][0]
flattened << hash_arg[:to][1]
end
final_args = flattened.select{|number| number.respond_to?(:integer?) }
raise ArgumentError unless final_args.length == 2 || final_args.length == 4
return measure_distance(*final_args)
end
def measure_distance(first_x, first_y, second_x=0, second_y=0)
x = first_x - second_x
y = first_y - second_y
return Math.sqrt((x*x) + (y*y))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment