[Screencast] Handling Nil in Crystal

First LuckyCast I’ve produced in a while, but it’s something that I remember hanging me up for quite a while when I first started learning Crystal. Hopefully, it’s helpful to some new folks getting started.


@stephendolan what format is the video?
I get the following error: “No video with supported format and MIME type found”

I’ll take a look, @kees ! In the meantime, I’ve swapped out my link for the YouTube version :slight_smile:

1 Like

Thanks, that works for me!

Nice video!

I think this is a recurring error that people trip into. That’s why we should probably put all this information into the compiler. It’s a bit tricky to determine how to do it, but here are some ideas:

  • If there’s an “undefined method” error on an instance variable that can be nil, check if that instance variable is part of an if statement that encloses this use. If so, suggest assigning it to a local variable.
  • Similarly, if it happens with a call, check if that call happens in an if. Or maybe in this case we could say “if you already checked whether current_user is nil or not before this line, make sure to first assign the result of that call to a local variable”

Thanks so much! In case it’s helpful reference material, here’s everything I could find on the topic on the forums and official GitHub repos while researching for the video: