As of Ruby 3.0, you can now define methods in one line (sometimes referred to as endless method definitions) such as the following:
def invalid? = !valid?
Here is a more detailed example from a recent blog post of mine on writing ActiveRecord Style Macros:
class Errors
def initialize
@h = Hash.new { |h, k| h[k] = [] }
end
def add(attribute, message) = (@h[attribute] << message)
def [](attribute) = @h[attribute]
def each(&block) = @h.each(&block)
def empty? = @h.all? { |_, v| v.empty? }
def any? = !empty?
def clear = @h.clear
def full_messages = @h.flat_map { |attribute, messages| messages.map { |message| "#{attribute} #{message}"} }
end
Use responsibly. 😅