Enumerables and collection manipulation

Ruby's Enumerable module provides rich collection methods. map transforms elements; select/reject filter. reduce aggregates values. find returns first match; find_all returns all matches. group_by partitions by criteria. partition splits into two arra

Custom validators and validation patterns

Rails validations ensure data integrity before persistence. I create custom validators for complex business rules. Validators inherit from ActiveModel::Validator or ActiveModel::EachValidator. EachValidator validates individual attributes; Validator v

Module mixins and concerns for code reuse

Ruby modules enable code sharing across classes without inheritance. I use include for instance methods, extend for class methods. prepend inserts module before class in method lookup. Concerns organize shared behavior—validations, scopes, association

ActiveRecord query optimization and N+1 prevention

ActiveRecord provides powerful query interface, but naive usage causes N+1 queries. includes eager loads associations in 2-3 queries. joins performs SQL JOINs for filtering. preload always uses separate queries; eager_load forces LEFT OUTER JOIN. I us

Blocks, Procs, and Lambdas for functional programming

Ruby's closures—blocks, procs, lambdas—enable functional programming patterns. Blocks are anonymous code chunks passed to methods. Procs are objects wrapping blocks, callable with call. Lambdas are stricter procs—check argument count and return differ

Background jobs with Sidekiq and ActiveJob

Sidekiq processes background jobs asynchronously using Redis and multi-threading. ActiveJob provides framework-agnostic interface—I use it for portability between job processors. Jobs handle emails, data processing, API calls, report generation. perfo

Advanced RSpec testing with shared examples

RSpec provides powerful testing tools for behavior-driven development. Shared examples reduce duplication across similar specs—I extract common behavior into reusable examples. Contexts organize tests by different scenarios. Let blocks lazily evaluate

Service objects for business logic encapsulation

Service objects extract complex business logic from models and controllers, following Single Responsibility Principle. I structure services with a clear public interface—typically a call method. Services handle multi-step operations, external API call

Metaprogramming with method_missing and define_method

Ruby's metaprogramming enables dynamic method definition and interception. method_missing catches undefined method calls, allowing DSL creation and proxy patterns. I implement it carefully with respond_to_missing? for proper introspection. define_meth