Created
December 27, 2017 23:24
-
-
Save onelharrison/c1ae60787c3c5be5fc33365bc436cb4e to your computer and use it in GitHub Desktop.
Code snippet showing how to define private class methods in Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Toyota < Car | |
def self.name | |
self.i_am | |
end | |
def self.i_am | |
"Toyota #{superclass}" | |
end | |
def self.do_something | |
# I want to be private too | |
puts "I'm private too! I do something." | |
end | |
# I added do_something to show that | |
# private_class_method takes an array | |
# of symbols corresponding to method | |
# names. | |
private_class_method :i_am, :do_something | |
end | |
# => Toyota.name | |
# "Toyota Car" | |
# | |
# => Toyota.i_am # expecting an error | |
# private method `i_am' called for Toyota:Class | |
# | |
# => Toyota.do_something # expecting an error | |
# private method `do_something' called for Toyota:Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment