I have a method that gets artists top albums from Lastfm
def get_albums
@albums = Array.new
@artistname.each do |name|
s = LastFM::Artist.get_top_albums(:artist => name, :limit => 1)
r = JSON.parse(s.to_json)['topalbums']['album']['name']
@albums.push r
end
end
This method work fine and returns an array of albums for each artist as long as the artist has a top album, if they don't I get a NoMethodError undefined method `[]' for nil:NilClass.
What I'm trying to do is add a check when parsing to try and get pass this NoMethodError but can't seem to figure it out. I tried this:
def get_albums
@albums = Array.new
@artistname.each do |name|
s = LastFM::Artist.get_top_albums(:artist => name, :limit => 1)
r = JSON.parse(s.to_json)['topalbums']['album']['name']
if['topalbums']['album']['name'].empty?
@albums.push(@artistname + "does not have a top album")
else
@albums.push r
end
end
end
Which gives me a TypeError can't convert String into Integer message
Any ideas how I could fix this? Thanks