how to access something within an array

B

bingo bob

I have this array...

[#<Yahoo::GeoPlanet::place:0x1031d3fb8 @bounding_box=[[42.554428,
1.52747], [42.536251, 1.50279]], @name="La Massana", @woe_id="472580",
@longitude=1.5153, @latitude=42.545052>]

How do I access the thing called @woe_id within it!

Whatever I try I can't seem to get at it?
 
M

Marnen Laibow-Koser

bingo said:
I have this array...

[#<Yahoo::GeoPlanet::place:0x1031d3fb8 @bounding_box=[[42.554428,
1.52747], [42.536251, 1.50279]], @name="La Massana", @woe_id="472580",
@longitude=1.5153, @latitude=42.545052>]

That array contains one entry, a Yahoo::GeoPlanet::place object.
How do I access the thing called @woe_id within it!

Whatever I try I can't seem to get at it?

Right. The @ at the beginning of the name tells you that it's an
instance variable (of the Place object). You can't directly get at an
object's instance variables from outside (except by using dangerous
tricks that I don't recommend), so check the rdoc for the Place class to
see if there's an accessor method or something that will give you the
woe_id.

Best,
 
P

Philippe Cantin

You should have an accessor , try : geo_planet_object.woe_id .

You can use introspection with :
geo_planet_object.instance_variable_get:)woe_id) ....
 
M

Marnen Laibow-Koser

Philippe said:
You should have an accessor , try : geo_planet_object.woe_id .

You can use introspection with :
geo_planet_object.instance_variable_get:)woe_id) ....

But don't. Introspection is dangerous in this case, and should not be
necessary. Just use the object's interface.

Best,
 
B

Brian Candler

bingo said:
I have this array...

[#<Yahoo::GeoPlanet::place:0x1031d3fb8 @bounding_box=[[42.554428,
1.52747], [42.536251, 1.50279]], @name="La Massana", @woe_id="472580",
@longitude=1.5153, @latitude=42.545052>]

How do I access the thing called @woe_id within it!

Whatever I try I can't seem to get at it?

You may get a hint from this:

thing = arr.first
p thing.methods - Object.methods
 
M

Marnen Laibow-Koser

Brian said:
bingo said:
I have this array...

[#<Yahoo::GeoPlanet::place:0x1031d3fb8 @bounding_box=[[42.554428,
1.52747], [42.536251, 1.50279]], @name="La Massana", @woe_id="472580",
@longitude=1.5153, @latitude=42.545052>]

How do I access the thing called @woe_id within it!

Whatever I try I can't seem to get at it?

You may get a hint from this:

thing = arr.first
p thing.methods - Object.methods

Good point, although that's not terribly reliable if method_missing
fakery is involved.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
J

Josh Cheek

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

I have this array...

[#<Yahoo::GeoPlanet::place:0x1031d3fb8 @bounding_box=[[42.554428,
1.52747], [42.536251, 1.50279]], @name="La Massana", @woe_id="472580",
@longitude=1.5153, @latitude=42.545052>]

How do I access the thing called @woe_id within it!

Whatever I try I can't seem to get at it?
It appears the object has a reader for woe_id (based on
http://github.com/mattt/yahoo-geopl...713f742849443d11/lib/yahoo-geoplanet/place.rb)
which would mean you could do array.first.woe_id

Of course, you'll need to consider whether the array will always have one
object, and whether you want to always pull it from that object (ie can the
array return nil? or what if it returns five of these objects, do you still
only want the woe_id of the first?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,428
Latest member
RosalieQui

Latest Threads

Top