Ruby and Keyword
last modified April 27, 2025
This tutorial explains how to use Ruby's and
keyword. It serves
as both a logical operator and a control flow tool in Ruby programs.
The and keyword evaluates multiple expressions, returning the first
falsey value or the last truthy value. It has lower precedence than &&
.
Unlike &&
, and
is useful for control flow rather than
boolean logic. It's commonly used for chaining operations where any failure
should stop execution.
Basic and Operator
This example shows the basic boolean operation with and
. It returns
true
only when both operands are truthy.
result = true and false puts result # => false result = 1 and "hello" puts result # => "hello" result = nil and 5 puts result.inspect # => nil
The and
operator evaluates expressions left-to-right. It returns
the first falsey value or the last value if all are truthy.
and vs && Precedence
The key difference between and
and &&
is precedence.
and
has much lower precedence, which affects evaluation order.
x = true && false puts x # => false y = true and false puts y # => true
The first example assigns the result of &&
to x
. The
second assigns true
to y
then evaluates and false
.
This demonstrates and
's lower precedence.
Control Flow with and
and
is often used for control flow where operations should continue
only if previous ones succeed. This example shows a common pattern.
def fetch_data # Simulate possible failure rand > 0.5 ? "data" : nil end data = fetch_data and process_data(data) puts "Data processed successfully" if data def process_data(data) puts "Processing: #{data}" end
The process_data
method only runs if fetch_data
returns truthy. This provides concise conditional execution.
Multiple Conditions with and
and
can chain multiple conditions together. Evaluation stops at the
first falsey value due to short-circuit behavior.
def valid_user?(user) user[:name] and user[:email] and user[:active] end user1 = { name: "John", email: "john@example.com", active: true } user2 = { name: "Alice", email: nil, active: true } puts valid_user?(user1) # => true puts valid_user?(user2) # => false
The method checks all required user attributes. It returns false
immediately if any check fails, making it efficient.
and for Assignment Guarding
and
can prevent assignment when a condition fails. This pattern
helps avoid overwriting variables with nil values.
def get_config_value # Simulate config lookup that might fail rand > 0.3 ? "admin" : nil end # Only assign if get_config_value returns truthy role = get_config_value and role = role.upcase puts role.inspect
The variable role
only gets assigned and modified if
get_config_value
returns a truthy value. This prevents nil errors.
and in Modifier Position
and
can be used in statement modifier position to conditionally
execute code. This provides a concise alternative to if
.
def log_in(user) puts "Logging in #{user[:name]}" user[:authenticated] = true end current_user = { name: "John", password: "secret" } authenticate(current_user) and log_in(current_user) def authenticate(user) user[:password] == "secret" end
The log_in
method only executes if authenticate
returns true. This pattern is common in authentication flows.
Error Handling with and
and
can chain operations with error handling. Each step executes
only if the previous one succeeds.
def read_file(path) File.exist?(path) and File.read(path) end def parse_json(data) JSON.parse(data) rescue nil end data = read_file("config.json") and config = parse_json(data) puts config ? "Config loaded" : "Failed to load config"
The example safely attempts to read and parse a file. Each operation proceeds only if the previous one succeeds, avoiding nested conditionals.
Source
This tutorial covered Ruby's and
keyword with practical examples
showing boolean logic, control flow, and error handling patterns.
Author
List all Ruby tutorials.