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

On Wed, Jun 2, 2010 at 1:30 AM, 12 34 <rubyforum(a)web.knobby.ws> wrote:

> 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"?
>
>
Because places is an array (think a list of geonames). You can tell it is an
Array because it is surrounded by brackets. You have to tell it which
element in the Array you are interested in, in this case the one at index
zero (the first index).

http://ruby-doc.org/core/classes/Array.html


> 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: 12 34 on
I'm confused. It's an array of one element, but that element has some
kind of parts. The "parts" and how you access them are not clear to me.
I can follow the model. It seems a hash would make more sense or
multiple elements in the array (except one couldn't access them by name.
And why an array of one element? Why bother with the array?

Confused, but thanks for the help. I do what I need to do, but
understanding it would help in the future.

At the moment I getting timeouts errors, so can't test much, but I did
earlier.

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

From: Michael Fellinger on
On Thu, Jun 3, 2010 at 8:44 AM, 12 34 <rubyforum(a)web.knobby.ws> wrote:
> I'm confused. It's an array of one element, but that element has some
> kind of parts. The "parts" and how you access them are not clear to me.

Toponym is just trying to make the access easier, you are right that
the structure is very Hash-alike, and I would argue that either a Hash
or a Struct would have been more sensible.
The author of the geonames library doesn't use much idomatic ruby
anywhere, so I will argue that he is just not very familiar with Ruby
and this library might even be his first project to be out in the
wild.

Now to Toponym itself, for the purpose of discussion I put the source
for this class online: http://pastie.org/990140
It's the kind of code that would make a seasoned Ruby developer run
screaming with waving hands.
Not only is he using 4 spaces for indentation ;) but also the
seldom-used attr in combination with attr_writer for the exact same
names, which would be much better handled with a single attr_accessor
statement.

module Geonames
class Toponym
attr_accessor :geoname_id, :name, :alternate_names,
:country_code, :country_name, :population,
:elevation, :feature_class, :feature_class_name,
:feature_code,:feature_code_name, :latitude,
:longitude, :distance
end
end


Same functionality, less boilerplate.
But to instantiate this class, Adam is using following code (which
warranted another pastie http://pastie.org/990146 )
As you can see, it's pretty verbose and unlike anything you would be
comfortable with after spending more time with Ruby.
Actually, after going through this code, I'm really itching to rewrite
it, we'll see how that goes :)

> I can follow the model. It seems a hash would make more sense or
> multiple elements in the array (except one couldn't access them by name.
> And why an array of one element? Why bother with the array?

Because it may return more than one toponym or none at all, having an
Array makes the result easier to handle.
I couldn't find any examples of coordinates that would return more
than one toponym.
Checking the source showed that it parses some XML, which may have
more than one, so although I cannot confirm that they exists they are
certainly allowed by the API.


> Confused, but thanks for the help. I do what I need to do, but
> understanding it would help in the future.
>
> At the moment I getting timeouts errors, so can't test much, but I did
> earlier.


--
Michael Fellinger
CTO, The Rubyists, LLC

From: 12 34 on
Michael

Thank you so much for taking the time to explain this. I had looked a
couple of my Ruby books and was finding nothing. I was ready to really
dig in this morning, but obviously would not have found the answer.

If you want a beta tester I'm in. I'm not a programmer of any kind and
can only write basic Ruby with lots of help. If you decide to go ahead
you might look at gps2photo and ExifTool. Another thing to consider is
what source is being used and offering alternatives. I think ExifTool
does this I think. We do need more mature gems to match what's available
in perl.

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

From: botp on
On Thu, Jun 3, 2010 at 7:44 AM, 12 34 <rubyforum(a)web.knobby.ws> wrote:
> I'm confused. It's an array of one element, but that element has some
> kind of parts. The "parts" and how you access them are not clear to me.

read about geonames

> I can follow the model. It seems a hash would make more sense or
> multiple elements in the array (except one couldn't access them by name.
> And why an array of one element? Why bother with the array?

because you can get possibly multiple places. if you want to get a lot
of places, specify a radius, possibly a big one just to test ..

kind regards -botp