From 03d929b440da96d1b3cb37713f550f4084785dc4 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Mon, 28 Mar 2011 21:49:55 -0400
Subject: [PATCH] Fix issue #43: don't alter array passed to method.

Reported by github.com/joelmats. The array passed to
Geocoder::Calculations.geographic_center was being modified in-place.
---
 lib/geocoder/calculations.rb | 2 +-
 test/geocoder_test.rb        | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/geocoder/calculations.rb b/lib/geocoder/calculations.rb
index 84f4e339..f2531922 100644
--- a/lib/geocoder/calculations.rb
+++ b/lib/geocoder/calculations.rb
@@ -120,7 +120,7 @@ module Geocoder
     def geographic_center(points)
 
       # convert objects to [lat,lon] arrays and remove nils
-      points.map!{ |p| p.is_a?(Array) ? p : p.to_coordinates }.compact
+      points = points.map{ |p| p.is_a?(Array) ? p : p.to_coordinates }.compact
 
       # convert degrees to radians
       points.map!{ |p| to_radians(p) }
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index bb1874c3..cad207d2 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -51,6 +51,14 @@ class GeocoderTest < Test::Unit::TestCase
     assert Geocoder.address(40.750354, -73.993371).is_a?(String)
   end
 
+  def test_geographic_center_doesnt_overwrite_argument_value
+    # this tests for the presence of a bug that was introduced in version 0.9.11
+    orig_points = [[52,8], [46,9], [42,5]]
+    points = orig_points.clone
+    Geocoder::Calculations.geographic_center(points)
+    assert_equal orig_points, points
+  end
+
 
   # --- general ---
 
-- 
GitLab