简体   繁体   English

有没有办法向用户返回零消息但不中断 ruby 中的应用程序?

[英]Is there a way to return nil message to user but not interrupt the app in ruby?

I'm trying to find a way to write in GUI for user that there is nothing in the button they just clicked我正在尝试为用户找到一种在 GUI 中编写的方法,即他们刚刚单击的按钮中没有任何内容

I've tried a method like `我试过像`这样的方法

if @album[@user_choice_album].tracks[10].name == nil
                return 

` But it interrupt the program and gave an message instead, while i was expecting it to interrupt the procedure and go to the next one. ` 但它中断了程序并给出了一条消息,而我期待它中断程序和 go 到下一个。

test.rb:192:in `draw_now_playing': undefined method `name' for nil:NilClass (NoMethodError)

            if (!(@album[@user_choice_album].tracks[10].name))\

It looks like you have tracks stored in an array (without knowing any details about your data structure and model).看起来您已将tracks存储在数组中(不知道有关数据结构和模型的任何详细信息)。 And when the user requests a track that doesn't exist, it returns nil .当用户请求不存在的曲目时,它返回nil

That means there is no point in asking if the name of that track is nil when the whole track is nil .这意味着当整个tracknil时,询问该轨道的name是否为 nil 是nil意义的。

I guess this should work for you:我想这应该适合你:

if @album[@user_choice_album].tracks[10].nil?
  return 

Or simplified:或者简化:

return unless @album[@user_choice_album].tracks[10]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM