Ruby | Conditionals and Flow Control

Answering Odin Project questions on Ruby Conditionals and Flow Control:

    • What is a “boolean”?
      A boolean is a binary variable that holds two possible values “true” and “false”
    • What are “truthy” values?
      Objects that are considered true in a boolean are truthy, false are considered falsey. What’s true and false varies across different languages.
    • Are nil, 0, "0", "", 1, [], {} and -1 considered true or false?
      nil -> false, 0->true, “0”->true, “”->true, 1->true, []->true, {}->true, -1->true
    • When do you use elsif?
      When you have more than two parts in an if statement.
    • When do you use unless?
      When you want to test for ‘false’.
    • What does <=> do?
      This ‘spaceship’ operator returns 1, 0, or -1 instead of 1, 0
      For example: a <=> b
      If a < b, return -1.
      If a = b, return 0.
      If a > b, return 1.2 <=> 5, returns -1 because 2 is less than 5.
    • Why might you define your own <=> method?
      When you are sorting an array. You can use it for both integers and strings. Strings are sorted according to the ASCII order and so is case sensitive. Uppercase letters come before lowercase letters.
    • What do || and && and ! do?
      || = logical ‘or’
      Evaluates the first operand. If true, returns true. Otherwise, returns the value of the second operand.

      && = logical ‘and’
      Evaluates the first operand. If false, returns false. Otherwise, returns the value of the second operand

      ! = ‘not’
      If the expression is true, returns false. If the expression is false, returns true.

    • What is returned by puts("woah") || true?
    • What is ||=?
      a ||= b would mean if ‘a’ is false, then set it equal to ‘b’. Otherwise, leave it the same value before running the conditional.
    • What is the ternary operator?
      ? :
      boolean_expression ? true_expression : false_expression
      Returns the true_expression if boolean_expression is true. Otherwise, returns false_expression.
    • When should you use a case statement?
      You should use a case statement when you want to match a value to a manageable number of conditions.

||= explanation:
Spaceship Operator:
Follow their Web Development course here:

One thought on “Ruby | Conditionals and Flow Control

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s