On Thu, Jun 3, 2010 at 8:44 AM, 12 34 [email protected] 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 ![:slight_smile: :slight_smile:](https://www.ruby-forum.com/images/emoji/apple/slight_smile.png?v=12)
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.