Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of `nil` private methods in ruby?

Tags:

ruby

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.

like image 523
Ulysse BN Avatar asked Jul 31 '18 10:07

Ulysse BN


People also ask

Why do we declare private methods Ruby?

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.

How do you use a private method in Ruby?

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).

What does private do in Ruby on Rails?

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.

Are private methods inherited in Ruby?

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.

What is a nil in Ruby?

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.

How do you send a message to a nil class in Ruby?

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.

Should I use public or private methods in Ruby?

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.

What is as a ruby method?

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.


1 Answers

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]
like image 65
Aleksei Matiushkin Avatar answered Sep 28 '22 11:09

Aleksei Matiushkin