In Ruby, the "if" statement is used to execute a block of code only if a certain condition is true. The syntax for the "if" statement looks like this:
main.rb62 chars4 lines
Here's an example:
main.rb101 chars8 lines
In this example, the "if" statement checks whether the age variable is greater than or equal to 18. If it is, the code inside the "if" block is executed, which prints "You are legally an adult." to the console.
You can also use an "else" block to provide code to execute if the condition is false:
main.rb135 chars10 lines
This example checks whether the age variable is greater than or equal to 18. If it is, the code inside the "if" block is executed. Otherwise, the code inside the "else" block is executed, which prints "You are still a minor." to the console.
gistlibby LogSnag