Calling the private method xxx using self.xxx () from another private method, resulting in a "private method` xxx" error called "

I am trying to get a good Ruby coding style. To avoid accidentally calling a local variable with the same name, I always use self.it where necessary. But now I came across this:

class MyClass < ActiveRecord::Base
  before_validation :sanitize_user_data

  private

  def sanitize_user_data
    self.sanitize_name # with ".self" it a problem, without it not!
  end

  def sanitize_name
    unless self.name.nil?
      self.name.gsub!(/\s+/, ' ')
      self.name.strip!
    end
  end
end

The above code results in an error

private method sanitize_namecalled

but when removed self.and just using sanitize_name, it works. Why?

+5
source share
2 answers

, , self.sanitize_name (self), sanitize_name, ( self).

, sanitize_name , self.send(:sanitize_name). , self " ", . , , :

def a; "method"; end
a = "variable"
a() #=> "method"
a   #=> "variable"
+8

?

. , , private.

+2

All Articles