0

I am using the youtube_it gem to retrieve a list of titles video ID's.

require 'youtube_it'
# query the video title
response = client.videos_by(:query => v, :max_results => 1)
# print out title
puts response.videos.first.title

An error occurs when it encounters a video which has been deleted.

undefined method `title' for nil:NilClass (NoMethodError)

How to handle this?

Solution

  # check if the video title exists
  v1 = response.videos.first
    if v1.nil?
      puts "*** VIDEO REMOVED ***"
    else
      # display video title
      puts v1.title
    end

Thanks.

4

1 回答 1

1

怎么样:

my_object = response.videos.first
if my_object.nil?
  puts "There is no object!"
else
  puts my_object.title
end

是零参考。

于 2013-08-14T09:46:59.083 回答