Skip to content

Instantly share code, notes, and snippets.

@mehulkar
Last active December 20, 2015 06:09
Show Gist options
  • Save mehulkar/6084232 to your computer and use it in GitHub Desktop.
Save mehulkar/6084232 to your computer and use it in GitHub Desktop.

protected methods can be called by all inherited classes, private methods can only be called by the class they're in.

In ruby, self.some_protected_method works, self.some_private_method doesn't work.

Here are examples

class Y
  def send_private;
    self.private_method_x
  end
  def send_protected;
    self.protected_method_x
  end
  
private
  def private_method_x; puts "hi I am private"; end

protected
  def protected_method_x; puts 'hi i am protected'; end
end
y = Y.new

y.send_private
NoMethodError: private method `private_method_x' called for #<Y:0x007f7febb99b38>
  from (irb):3:in `a'
	from (irb):11

y.send_protected
hi i am protected
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment