Press "Enter" to skip to content

Re: More Fluent Method Signatures in Ruby

Shane Becker

Is this a hare brained /#Rails thought?

What if we named `type` with adverbs?

adjective (typical):
user.sync :full
user.sync :incremental

adverbs:
user.sync :fully
user.sync :incrementally

It reads more like a proper sentence to my eyes.
Instead of a Programmering Sentence™

December 14, 2025, 4:57 am 1 boosts 0 favorites

My Reply

I like this a lot. It makes me want to go even harder:

user.sync_fully
user.sync_incrementally

You could do this a number of ways (TMTOWTDI)…

Hard-code the methods

 class User
   def sync(type, *rest)
     # ...
   end
   def sync_fully(*rest) = sync :fully, *rest
   def sync_incrementally(*rest) = sync :incrementally, *rest
 end

Use method_missing

class User
  def sync(type, *rest)
    # ...
  end
  def method_missing(method_name, *rest)
    super unless respond_to?(method_name)
    sync(method_name[5..-1], *rest)
  end
  def respond_to_missing?(method_name, include_private = false)
    method_name.start_with?("sync_")
  end
end

Use Metaprogramming

class User
  TYPES = %i[fully, incrementally]
  def sync(type, *rest)
    raise ArgumentError unless TYPES.include?(type)
    # ...
  end
  TYPES.each do |type|
    define_method("sync_#{type}") do |*rest|
      sync(type, *rest)
    end
  end
end

5 Comments

  1. @kerrick I'm curious… does your indieweb post on your site format my post/note's content if I use code fences with backticks?

    “`ruby
    puts 'this is a test'
    “`

Reposts

Leave a Reply

Your email address will not be published. Required fields are marked *


To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)