Using ruby 2.3, and pry REPL, I have this funny result I cannot understand:
nil.private_methods
# [:DelegateClass, :Digest, :sprintf, :format, :Integer, :Float, :String, :Array, :Hash, :throw, :iterator?, :block_given?, :catch, :loop, :Rational, :trace_var, :untrace_var, :Complex, :at_exit, :gem_original_require, :URI, :set_trace_func, :select, :caller, :caller_locations, :test, :fork, :`, :exit, :sleep, :respond_to_missing?, :load, :exec, :exit!, :syscall, :open, :printf, :print, :putc, :puts, :readline, :readlines, :p, :abort, :gets, :system, :spawn, :proc, :lambda, :srand, :pp, :rand, :initialize_copy, :initialize_clone, :initialize_dup, :Pathname, :trap, :gem, :BigDecimal, :require, :require_relative, :autoload, :autoload?, :binding, :local_variables, :warn, :raise, :fail, :global_variables, :__method__, :__callee__, :__dir__, :eval, :method_missing, :singleton_method_added, :singleton_method_removed, :singleton_method_undefined, :initialize]
It looks likes those private methods are linked to modules loaded:
methods = nil.private_methods.dup
require "json"
nil.private_methods - methods
# [:j, :JSON, :jj]
I'm wondering about the purpose of those private methods and didn't find anything relevant on the internet.
What is a private method in Ruby? It's a type of method that you can ONLY call from inside the class where it's defined. This allows you to control access to your methods.
Methods that have private visibility implement the internal logic of the object. They can be called inside the same class in which they are defined, or inside derived classes. Unlike protected methods, you can call them only for a current instance of the class (you can't explicitly specify the method receiver).
Private: – When a method is declared private in Ruby, it means this method can never be called with an explicit receiver. Any time we're able to call a private method with an implicit receiver.
In general, private methods can't be inherited in object-oriented programming languages. But in Ruby, private methods can also be inherited just like protected and public methods. The public method can be accessed outside the class in which they are defined.
GeeksforGeeks ! In Ruby, nil is a special value that denotes the absence of any value. Nil is an object of NilClass. nil is Ruby’s way of referring to nothing or void. Ruby also provide a nil? method to detect if any object is nil or not.
When a message is sent to nil, a hard-coded C-level “class” called rb_cNilClass — which corresponds to the NilClass in Ruby — is used as receiver of the message. Then the message will be found and sent among the instance methods of the new receiver.
Only if you have a very specific case, like the equals ( ==) method. The Ruby documentation recommends using private instead of protected whenever possible. “A protected method is slow because it can’t use inline cache.” That’s a difference of 8.5% in performance. You’ve learned about Ruby method visibility, public, private & protected methods.
As a Ruby method is — behind the scene… | by Tech - RubyCademy | RubyCademy | Medium It’s all under control.. As a Ruby method is — behind the scene — a message handler associated with a block of instructions that returns an object, the private and protected policies are strongly correlated with the Ruby message concept.
NilClass
does not declare any private instance method on it’s own. They came from Kernel
and BasicObject
:
NilClass.ancestors
#⇒ [NilClass, Object, PP::ObjectMixin, Kernel, BasicObject]
NilClass.private_instance_methods -
(NilClass.ancestors - [NilClass]).flat_map(&:private_instance_methods)
#⇒ []
Any ruby class is derived from BasicObject
and includes Kernel
.
Also, Module#private_instance_methods
accepts the boolean argument, which suppresses the output of inherited methods when set to false
:
NilClass.private_instance_methods false
#⇒ []
JSON
on the other hand monkey patches NilClass
amongst all other classes:
require 'json'
NilClass.ancestors
#⇓ [NilClass, JSON::Ext::Generator::GeneratorMethods::NilClass,
# Object, JSON::Ext::Generator::GeneratorMethods::Object,
# PP::ObjectMixin, Kernel, BasicObject]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With