简体   繁体   中英

is there a way to convert an array in Ruby to a float by removing the brackets?

I have a little problem that seems rather easy to solve but I can't seem to find a solution. I'm using the Geocoder gem for Rails and from this method of:

Geocoder.coordinates(params[:search])

I'm getting an array that contains floats like this: [43.653226, -79.3831843]

My question is, is there a way to remove the brackets from the array or convert the value to a float so the output is 43.653226, -79.3831843 ?

My closest solution so far has been to do this:

a = Geocoder.coordinates(params[:search])

and then to obtain each float individually by doing this in the method call:

a[0] --> 43.653226
a[-1] --> -79.3831843

This doesn't seem to work as I get an error with the particular method that I am trying to use.

I'm using the 'sunspot-solr' and 'sunspot-rails' gem for this and what I am trying to do is allow a user to enter a city to find users and then display users near that city within a 100, 200 km radius

This is what I have in my user.rb file:

class User < ActiveRecord::Base

    searchable do
        text :city
        latlon(:location) { Sunspot::Util::Coordinates.new(latitude, longitude) }
    end

end

This is what's in my users_controller.rb

def index
    if params[:search]
      @search = User.search do
        fulltext params[:search]
        with(:location).near(*Geocoder.coordinates(params[:search]),:precision => 6)
      end
      @users = @search.results
    end
end

This is the line that seems to be causing the problem: with(:location).near(*Geocoder.coordinates(params[:search]),:precision => 6)

And finally in my view, this is the search form:

<%= form_tag users_path, class: "form-signin", role: "form", method: :get do %>

    <%= text_field_tag :search, params[:search], class: "form-control", placeholder:"Type in a location" %>

    <div><%= submit_tag "Search users", class: "btn btn-lg btn-primary btn-block", id: "home-search" %></div>

<% end %>

Now when I'm trying to add the geospatial feature I'm getting a 400 error that looks like this:

RSolr::Error::Http - 400 Bad Request Error: {'responseHeader'=>{'status'=>400,'QTime'=>2},'error'=>{'msg'=>'com.spatial4j.core.exception.InvalidShapeException: incompatible dimension (2) and values (dpz83dffmxps). Only 0 values specified','code'=>400}}

If anyone can help that'd me great!

The values are floats already, if you want to use the content of the array as 2 arguments to a method call then you can use the splat operator ( * ) eg

method_call(*[43.653226, -79.3831843]) is same as method_call(43.653226, -79.3831843)

or destructure the array and pass the new variables as argument eg:

lat, lng = [43.653226, -79.3831843]
method_call(lat,lng)

You could use join: eg.

a = Geocoder.coordinates(params[:search])
a.join(", ");

Would return the array - comma separated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM