Project

General

Profile

Actions

Feature #18618

closed

no clobber def

Added by ed_ (Ed Mangimelli) 5 months ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:107806]

Description

Sometimes I want to be certain I'm not clobbering/masking a method:

class Dog
  def bark
    'bark!'
  end
end

class Poodle < Dog
  raise if method_defined? :bark
  def bark
    'bow-wow'
  end
end

I propose creating a shorthand. Maybe something like:

class Dog
  def bark
    'bark!'
  end
end

class Poodle < Dog
  ncdef bark        # "no clobber" def
    'bow-wow'
  end
end

=> #<MethodAlreadyDefined: Method `bark' already defined.>

This would be useful in scenarios where subclassing a class (or including a mixin) you don't own is common practice --for instance, subclassing ApplicationRecord for your model in Rails.

I agree that ncdef is pretty ugly. Maybe def!

Actions

Also available in: Atom PDF