Tutorial by Examples

Ruby offers the expected if and else expressions for branching logic, terminated by the end keyword: # Simulate flipping a coin result = [:heads, :tails].sample if result == :heads puts 'The coin-toss came up "heads"' else puts 'The coin-toss came up "tails"' end ...
In Ruby, there are exactly two values which are considered "falsy", and will return false when tested as a condition for an if expression. They are: nil boolean false All other values are considered "truthy", including: 0 - numeric zero (Integer or otherwise) "&qu...
A while loop executes the block while the given condition is met: i = 0 while i < 5 puts "Iteration ##{i}" i +=1 end An until loop executes the block while the conditional is false: i = 0 until i == 5 puts "Iteration ##{i}" i +=1 end
A common pattern is to use an inline, or trailing, if or unless: puts "x is less than 5" if x < 5 This is known as a conditional modifier, and is a handy way of adding simple guard code and early returns: def save_to_file(data, filename) raise "no filename given" if fi...
A common statement is if !(some condition). Ruby offers the alternative of the unless statement. The structure is exactly the same as an if statement, except the condition is negative. Also, the unless statement does not support elsif, but it does support else: # Prints not inclusive unless 'hell...
Ruby uses the case keyword for switch statements. As per the Ruby Docs: Case statements consist of an optional condition, which is in the position of an argument to case, and zero or more when clauses. The first when clause to match the condition (or to evaluate to Boolean truth, if the condi...
The flow of execution of a Ruby block may be controlled with the break, next, and redo statements. break The break statement will exit the block immediately. Any remaining instructions in the block will be skipped, and the iteration will end: actions = %w(run jump swim exit macarena) index = 0 ...
Unlike many other programming languages, the throw and catch keywords are not related to exception handling in Ruby. In Ruby, throw and catch act a bit like labels in other languages. They are used to change the control flow, but are not related to a concept of "error" like Exceptions are...
While it might seem counterintuitive, you can use logical operators to determine whether or not a statement is run. For instance: File.exist?(filename) or STDERR.puts "#{filename} does not exist!" This will check to see if the file exists and only print the error message if it doesn't....
The begin block is a control structure that groups together multiple statements. begin a = 7 b = 6 a * b end A begin block will return the value of the last statement in the block. The following example will return 3. begin 1 2 3 end The begin block is useful for conditi...
Consider this broken snippet: def foo bar = [1, 2, 3, 4].map do |x| return 0 if x.even? x end puts 'baz' bar end foo # => 0 One might expect return to yield a value for map's array of block results. So the return value of foo would be [1, 0, 3, 0]. Instead, return retu...
Ruby has an or-equals operator that allows a value to be assigned to a variable if and only if that variable evaluates to either nil or false. ||= # this is the operator that achieves this. this operator with the double pipes representing or and the equals sign representing assigning of a valu...
Ruby has a ternary operator (?:), which returns one of two value based on if a condition evaluates as truthy: conditional ? value_if_truthy : value_if_falsy value = true value ? "true" : "false" #=> "true" value = false value ? "true" : "fals...
The flip flop operator .. is used between two conditions in a conditional statement: (1..5).select do |e| e if (e == 2) .. (e == 4) end # => [2, 3, 4] The condition evaluates to false until the first part becomes true. Then it evaluates to true until the second part becomes true. After t...

Page 1 of 1