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

Viewed 1811

I'm trying to get a good Ruby coding style. To prevent accidently calling a local variable with the same name, I'm always using self. where appropriate. But now I stumbled over this:

class MyClass < ActiveRecord::Base
  before_validation :sanitize_user_data

  private

  def sanitize_user_data
    self.sanitize_name # with ".self" it's a problem, without it's 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_name called

but when removing self. and just using sanitize_name, it works. Why's that?

2 Answers
Related