# How to access something within an array

**URL:** <https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963>\
**Category:** Ruby\
**Created:** [January 2, 2010, 8:15am UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963 "2010-01-02T08:15:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 2, 2010, 8:15am UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963/1 "2010-01-02T08:15:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 2, 2010, 9:16am UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963/2 "2010-01-02T09:16:53Z")

</div>

bingo bob wrote:

> 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,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 2, 2010, 12:48pm UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963/3 "2010-01-02T12:48:57Z")

</div>

You should have an accessor , try : geo\_planet\_object.woe\_id .

You can use introspection with :  
geo\_planet\_object.instance\_variable\_get(:woe\_id) …

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 2, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963/4 "2010-01-02T18:49:31Z")

</div>

Philippe Cantin wrote:

> 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,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 2, 2010, 7:34pm UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963/5 "2010-01-02T19:34:29Z")

</div>

bingo bob wrote:

> 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

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 2, 2010, 9:10pm UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963/6 "2010-01-02T21:10:49Z")

</div>

Brian C. wrote:

> bingo bob wrote:
> 
> > 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 3, 2010, 5:03am UTC](https://www.ruby-forum.com/t/how-to-access-something-within-an-array/180963/7 "2010-01-03T05:03:25Z")

</div>

On Sat, Jan 2, 2010 at 1:15 AM, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

It appears the object has a reader for woe\_id (based on  
[http://github.com/mattt/yahoo-geoplanet/blob/6c073d969191e1f03e7d5523713f742849443d11/lib/yahoo-geoplanet/place.rb](http://github.com/mattt/yahoo-geoplanet/blob/6c073d969191e1f03e7d5523713f742849443d11/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?)
