How do i pull elements out of this array
>> events.first
=> #<Bandsintown::Event:0x1037caf98 @venue=#<B开发者_如何学Goandsintown::Venue:0x1037c9580 @region="Australia", @bandsintown_id="160698", @latitude="-37.8161090", @city="Melbourne Vic", @bandsintown_url="http://www.bandsintown.com/venue/160698", @longitude="144.9725040", @country="Australia", @name="Rod Laver Arena at Melbourne Park">, @bandsintown_id="3671194", @on_sale_datetime=nil, @artists=[#<Bandsintown::Artist:0x1037c9328 @bandsintown_url="http://www.bandsintown.com/Metallica", @mbid="65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab", @name="Metallica">], @ticket_status="available", @bandsintown_url="http://www.bandsintown.com/event/3671194", @ticket_url="http://www.bandsintown.com/event/3671194/buy_tickets", @status=nil, @datetime=Wed Sep 15 19:00:00 -0400 2010>
> events.first[:region]
NoMethodError: undefined method `[]' for #<Bandsintown::Event:0x1037caf98>
>> events.first.region
NoMethodError: undefined method `region' for #<Bandsintown::Event:0x1037caf98>
Am i missing something
events
is not an array, it is an Bandsintwon::Event
object. Furthermore, region
is a property of venue
, which is another object within events
, of type Bandsintown::Venue
. Without having the classes for reference, it's possible that there might be venue
and region
methods that you can use to get the @venue
and @region
instance variables:
events.first.venue.region
Failing that, you could use instance_variable_get
to get the value directly:
region = events.first.instance_variable_get("@venue").instance_variable_get("@region")
... but it's probably better to use a method (so you should add one if it's your class and one doesn't exist!)
events.first.venue.region
精彩评论