From: 12 34 on
A Ruby newbie having trouble getting results back from geonames

#!/usr/bin/env ruby
require "rubygems"
require 'geonames'

places_nearby = Geonames::WebService.find_nearby_place_name
41.505928,-81.594582
p places_nearby # --> [#<Geonames::Toponym:0x1021042f0
@country_code="US", @feature_code_name=nil, @elevation=nil,
@distance=0.4495, @alternate_names=nil, @feature_code="PPLX",
@population=nil, @geoname_id="5161005", @longitude=-81.5981817,
@feature_class_name=nil, @country_name="United States", @name="Little
Italy", @latitude=41.5089406, @feature_class="P">]

How do I get any of the values, e.g., country_name? places_nearby is a
local variable.

Thank you in advance for help. I've tried several things, but can't
figure it out.
--
Posted via http://www.ruby-forum.com/.

From: Sven Schott on
[Note: parts of this message were removed to make it a legal post.]

Have you tried just calling the name of the instance var? Sometimes nice
developer set up easy setters and getters in their implementations.

e.g.

places_nearby[0].country_code

Just a thought. What are you trying that's failing? Have you looked through
the docs at

http://www.tbcn.ca/ruby_geonames

On Wed, Jun 2, 2010 at 3:57 PM, 12 34 <rubyforum(a)web.knobby.ws> wrote:

> A Ruby newbie having trouble getting results back from geonames
>
> #!/usr/bin/env ruby
> require "rubygems"
> require 'geonames'
>
> places_nearby = Geonames::WebService.find_nearby_place_name
> 41.505928,-81.594582
> p places_nearby # --> [#<Geonames::Toponym:0x1021042f0
> @country_code="US", @feature_code_name=nil, @elevation=nil,
> @distance=0.4495, @alternate_names=nil, @feature_code="PPLX",
> @population=nil, @geoname_id="5161005", @longitude=-81.5981817,
> @feature_class_name=nil, @country_name="United States", @name="Little
> Italy", @latitude=41.5089406, @feature_class="P">]
>
> How do I get any of the values, e.g., country_name? places_nearby is a
> local variable.
>
> Thank you in advance for help. I've tried several things, but can't
> figure it out.
> --
> Posted via http://www.ruby-forum.com/.
>
>

From: Michael Fellinger on
On Wed, Jun 2, 2010 at 2:57 PM, 12 34 <rubyforum(a)web.knobby.ws> wrote:
> 41.505928,-81.594582

iota ~ % irb
>> require 'geonames'
=> true
>> places = Geonames::WebService.find_nearby_place_name(41.505928, -81.594582)
=> [#<Geonames::Toponym:0x000000027dd158 @name="Little Italy",
@alternate_names=nil, @latitude=41.5089406, @longitude=-81.5981817,
@geoname_id="5161005", @country_code="US", @country_name="United
States", @feature_class="P", @feature_code="PPLX",
@feature_class_name=nil, @feature_code_name=nil, @population=nil,
@elevation=nil, @distance=0.4495>]
>> places.first.country_name
=> "United States"

--
Michael Fellinger
CTO, The Rubyists, LLC

From: 12 34 on
Michael Fellinger wrote:
> On Wed, Jun 2, 2010 at 2:57 PM, 12 34 <rubyforum(a)web.knobby.ws> wrote:
>> 41.505928,-81.594582
>
> iota ~ % irb
>>> require 'geonames'
> => true
>>> places = Geonames::WebService.find_nearby_place_name(41.505928, -81.594582)
> => [#<Geonames::Toponym:0x000000027dd158 @name="Little Italy",
> @alternate_names=nil, @latitude=41.5089406, @longitude=-81.5981817,
> @geoname_id="5161005", @country_code="US", @country_name="United
> States", @feature_class="P", @feature_code="PPLX",
> @feature_class_name=nil, @feature_code_name=nil, @population=nil,
> @elevation=nil, @distance=0.4495>]
>>> places.first.country_name
> => "United States"

Thanks. I had tried places.country_name among others. Why the "first"?

places_nearby[0].country_code works too.

The docs didn't suggest how to parse the result.

Thank you for the answers, but I'm not sure why it's working. But I can
proceed.

--
Posted via http://www.ruby-forum.com/.

From: botp on
On Wed, Jun 2, 2010 at 2:30 PM, 12 34 <rubyforum(a)web.knobby.ws> wrote:
> Thanks. I had tried places.country_name among others. Why the "first"?
> places_nearby[0].country_code works too.
> The docs didn't suggest how to parse the result.

try playing w ruby using irb,

places_nearby.class
places_nearby.each_with_index{|x,i| puts"#{i}) #{x}"}
places_nearby[0]
places_nearby.size
places_nearby[0].class
places_nearby[0].methods


> Thank you for the answers, but I'm not sure why it's working. But I can
> proceed.

welcome to ruby.
kind regards -botp