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 2 boosts 3 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'
    “`

    • @veganstraightedge @[email protected]

      Looks like it doesn't. Oddly, it seems to think you want “

      • @kerrick @[email protected] that's rude of it 😬

        Are you using a Markdown parser on the post content? Is it Kramdown?

        • @veganstraightedge @[email protected] It's actually just a WordPress blog with the Automattic's ActivityPub plugin [0]. The code in my post body is syntax highlighted by another plugin [1].

          [0]: https://wordpress.org/plugins/activitypub/
          [1]: https://wordpress.org/plugins/syntaxhighlighter/

Reposts

  • Kerrick Long (code)

Leave a Reply

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

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.