End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Module#private_class_method
mod.private_class_method(symbol, ...) → mod
mod.private_class_method(string, ...) → mod
mod.private_class_method(array) → mod
vm_method.c
View on GitHub ↗
static VALUE
rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
{
set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PRIVATE);
return obj;
}
Makes existing class methods private. Often used to hide the default constructor new.
String arguments are converted to symbols. An Array of Symbols and/or Strings are also accepted.
Example
class SimpleSingleton # Not thread safe private_class_method :new def SimpleSingleton.create(*args, &block) @me = new(*args, &block) if ! @me @me end end
Ruby version changes
-
3.0
-
Module#public,Module#protected,Module#private,Module#public_class_method,Module#private_class_method, toplevel “private” and “public” methods now accept single array argument with a list of method names. [Feature #17314]
-
See also
#private
#private_constant
#prepend_features
#private_instance_methods
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1