Skip to content

Instantly share code, notes, and snippets.

@rentalcustard
Created October 30, 2012 08:43
Show Gist options
  • Save rentalcustard/3979048 to your computer and use it in GitHub Desktop.
Save rentalcustard/3979048 to your computer and use it in GitHub Desktop.
Ruby private methods
class PrivacyExample
def some_public_method
self.some_private_method
end
def some_private_method
"o hai"
end
private :some_private_method
end
#> PrivacyExample.new.some_public_method
#NoMethodError: private method `some_private_method' called for #<PrivacyExample:0x007f98c48f4800>
# from (irb):3:in `some_public_method'
# from (irb):12
# from /Users/tomstuart/.rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in `<main>'
class PrivacyExample
def some_public_method
some_private_method
end
def some_private_method
"o hai"
end
private :some_private_method
end
#> PrivacyExample.new.some_public_method
# => "o hai"
#of course, setters are completely different...
class PrivacyExample
def some_var=(some_val)
@some_var = some_val
end
private :some_var=
def some_public_method
some_var = "foo"
@some_var
end
end
#1.9.2p320 :011 > PrivacyExample.new.some_public_method
# => nil
#some_var in some_public_method shadows the setter, so @some_var is still nil.
class PrivacyExample
def some_var=(some_val)
@some_var = some_val
end
private :some_var=
def some_public_method
#should fail - calling a private method with explicit receiver.
self.some_var = "foo"
@some_var
end
end
#1.9.2p320 :018 > PrivacyExample.new.some_public_method
# => "foo"
#wat.
@rentalcustard
Copy link
Author

it's ok guys i have a fix

class Module
  def private(*args)
    protected(*args)
  end
end

@rentalcustard
Copy link
Author

Although actually that's no good either, because protected allows you to call the method from any instance of the defining class (or, I think, any instance of an object that includes the defining module). Which is something additional to private. So yea.

lolruby.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment