Ruby and keyword tutorial explains how to use this logical operator with practical examples.
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.
This example shows the basic boolean operation with and. It returns true only when both operands are truthy.
basic_and.rb
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.
The key difference between and and && is precedence. and has much lower precedence, which affects evaluation order.
precedence.rb
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.
and is often used for control flow where operations should continue only if previous ones succeed. This example shows a common pattern.
control_flow.rb
def fetch_data
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.
and can chain multiple conditions together. Evaluation stops at the first falsey value due to short-circuit behavior.
multiple_conditions.rb
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 can prevent assignment when a condition fails. This pattern helps avoid overwriting variables with nil values.
assignment_guard.rb
def get_config_value
rand > 0.3 ? “admin” : nil end
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 can be used in statement modifier position to conditionally execute code. This provides a concise alternative to if.
modifier_position.rb
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.
and can chain operations with error handling. Each step executes only if the previous one succeeds.
error_handling.rb
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.
This tutorial covered Ruby’s and keyword with practical examples showing boolean logic, control flow, and error handling patterns.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Ruby tutorials.