concept expression in category ruby

appears as: expressions, n expression, The expression, expression, expression, expressions
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

Names of methods in Ruby follow the same rules and conventions as local variables (except that they can end with ?, !, or =, with significance that you’ll see later). This is by design: methods don’t call attention to themselves as methods but rather blend into the texture of a program as expressions that provide a value. In some contexts you can’t tell just by looking at an expression whether you’re seeing a local variable or a method name—and that’s intentional.

  • Conditional execution—Execution depends on the truth of an expression.
  • This chapter has covered several wide-ranging topics, bundled together because they have in common the fact that they involve control flow. Conditionals move control around based on the truth or falsehood of expressions. Loops repeat a segment of code unconditionally, conditionally, or once for each item in a list. Iterators—methods that yield to a code block you provide alongside the call to the method—are among Ruby’s most distinctive features. You’ve learned how to write and call an iterator, techniques you’ll encounter frequently later in this book (and beyond).

    Listing 7.1. Testing the Boolean value of expressions using if constructs
    if (class MyClass end)                      #1
      puts "Empty class definition is true!"
    else
      puts "Empty class definition is false!"
    end
    if (class MyClass; 1; end)                  #2
      puts "Class definition with the number 1 in it is true!"
    else
      puts "Class definition with the number 1 in it is false!"
    end
    if (def m; return false; end)               #3
      puts "Method definition is true!"
    else
      puts "Method definition is false!"
    end
    if "string"                                 #4
      puts "Strings appear to be true!"
    else
      puts "Strings appear to be false!"
    end
    if 100 > 50                                 #5
      puts "100 is greater than 50!"
    else
      puts "100 is not greater than 50!"
    end
    
    
    !@%STYLE%@!
    {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":44},{\"line\":0,\"ch\":46}],[{\"line\":5,\"ch\":44},{\"line\":5,\"ch\":46}],[{\"line\":10,\"ch\":44},{\"line\":10,\"ch\":46}],[{\"line\":15,\"ch\":44},{\"line\":15,\"ch\":46}],[{\"line\":20,\"ch\":44},{\"line\":20,\"ch\":46}]]"}
    !@%STYLE%@!