Comment

Comment by nottombrown

In Swift, you have to signal and guard against the nil.

In ruby, you choose whether to signal and you choose whether to guard. Here are some ruby examples that may be more clear:

Does not signal

    user = User.find_by_favorite_dinosaur("Dromiceiomimus")
Does not guard

    def print_favorite_dinosaur(user)
        p "User's favorite dinosaur is #{user.dino})"
    end
Signals and guards

    def print_favorite_dinosaur(user)
        if user
            p "User's favorite dinosaur is #{user.dino})"
        else
            p "No user. (Let's assume she likes T-rexs)"
        end
    end

    possible_user = User.find_by_favorite_dinosaur("Dromiceiomimus")

    print_favorite_dinosaur(possible_user)
parent nottombrown 12 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton