hhanzo1
hhanzo1

Reputation: 657

Handling deleted videos with the youtube_it gem

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.

Upvotes: 0

Views: 157

Answers (1)

ajt
ajt

Reputation: 552

how about:

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

is nil ref.

Upvotes: 1

Related Questions