Blocks
Build a big expertise of Ruby –
One small block of knowledge at a time.
AppSignal Ignore Errors YML
Let jobs die without yelling
You can add the key ignore_errors
to config/appsignal.yml
and list any error class you don't want reported.
...
ignore_errors:
CustomIgnoredError
...
For example if want Sidekiq jobs to fail using the normal mechanisms but not clog your logs.
Array Decomposition
Elegant Array Handling in Ruby
Array decomposition allows you to extract values from arrays into individual variables. The splat operator (*
) lets you collect remaining elements.
# Simple decomposition
first, second, third = [1, 2, 3]
# With splat operator
first, *rest = [1, 2, 3, 4, 5]
# first = 1
# rest = [2, 3, 4, 5]
Great for method arguments, array manipulation, and working with fixed-length arrays. Use it to make your code more readable and maintainable.
Double Splat Operator
Flexible Hash Arguments in Ruby
The double splat operator (**
) allows methods to accept an arbitrary number of keyword arguments as a hash. Perfect for creating flexible APIs and handling optional parameters.
def greet(name:, age:, **options)
puts "Hello #{name}, you are #{age} years old"
puts "Additional info: #{options}" if options.any?
end
# Call with extra arguments
greet(name: "John", age: 30, city: "New York", occupation: "Developer")
Use it when you need to pass through additional options or create flexible method signatures. Great for API wrappers, configuration objects, and method delegation.
Method Missing
Dynamic Method Handling in Ruby
The method_missing
hook allows you to handle calls to undefined methods. It's perfect for creating dynamic APIs and implementing flexible interfaces.
class User
def method_missing(method_name, *args)
if method_name.to_s.start_with?('find_by_')
attribute = method_name.to_s.split('find_by_').last
where(attribute => args.first)
else
super
end
end
end
# Now you can do:
User.find_by_name("John")
Great for creating dynamic finders, API wrappers, and configuration objects. Remember to implement respond_to_missing?
and always call super
for unhandled methods.
Safe Navigation Operator
Safely Handling Nil Objects in Ruby
The safe navigation operator (&.
) provides a concise way to handle potentially nil objects. It returns nil instead of raising a NoMethodError when encountering nil.
# Without safe navigation
if user && user.profile && user.profile.address
puts user.profile.address
end
# With safe navigation
puts user&.profile&.address
Perfect for dealing with optional associations, API responses, and database records that might not exist. Use it to write more elegant and defensive code.
The Tap Method
A Clean Way to Chain Object Modifications
The tap
method yields an object to a block and returns the object itself. It's perfect for performing operations on an object while maintaining a clean chain.
# Without tap
user = User.new
user.name = "John"
user.save
return user
# With tap
User.new.tap do |user|
user.name = "John"
user.save
end
Great for object initialization, debugging method chains, and keeping your code DRY. Use it when you need to perform multiple operations on an object while maintaining method chaining.