This refactoring pattern is very simple but I found it very useful in every app I was working on. You can use it wherever class or method has to much code. I used to stick to these rules when creating services:

  • use service suffix in every service class name
  • respect single responsibility principle but it’s more a general rule than something specific to the service pattern

Let’s build some sample service class to demonstrate idea behind it:

module Users
  class EmailDomainService
    def initialize(user)
      @user = user

    def email_domain"@").last if

    attr_reader :user

Want to receive useful tips, information about new Ruby gems and articles on a daily basis? Make sure you follow me and say hello!

It’s a very simple and small but very useful class. You shouldn’t have many lines of code inside your services. They should do only small things, serve only one thing at once.

You can use this pattern when you will be refactoring large class by moving small portions of code to separated services.

Want to become a better Rails developer?
Download for free the Introduction Rails patterns book and dive into the world of refactoring and easy-testable Ruby code today.
Join over 1,000 developers already subscribed to my newsletter and download the book. You can unsubscribe anytime:

Subscribe and get the book!