【问题标题】:Handling deleted videos with the youtube_it gem使用 youtube_it gem 处理已删除的视频
【发布时间】:2013-08-14 09:33:05
【问题描述】:

我正在使用 youtube_it gem 来检索标题视频 ID 的列表。

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

遇到已删除的视频会出错。

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

如何处理?

解决方案

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

谢谢。

【问题讨论】:

标签: ruby youtube-api


【解决方案1】:

怎么样:

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

is nil ref.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2020-02-26
    相关资源
    最近更新 更多