rubylibgosu

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

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.

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

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

Solution

  • It looks like you have tracks stored in an array (without knowing any details about your data structure and model). And when the user requests a track that doesn't exist, it returns nil.

    That means there is no point in asking if the name of that track is nil when the whole track is 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]