Last active
August 29, 2015 14:02
-
-
Save cupakromer/023278290dddebd785a6 to your computer and use it in GitHub Desktop.
"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 Book | |
# This is a "class" method | |
def Book.catch_em_all | |
"I'm sorry, you can't do that Dave" | |
end | |
# This is also a "class" method, since "self" right now is the | |
# Class object instance for this "class" | |
def self.bind_it(spell) | |
"We're still working on binding books with spell." | |
end | |
class << self | |
# This is another "class" method, now we have changed into the | |
# eigen-"class" of this "Book" Class object instance. | |
def my_mind_hurts | |
"Stop gulping frozen water!" | |
end | |
def delegate_to_secret | |
only_the_book_knows | |
end | |
private | |
# This is a private "class" method!! | |
def only_the_book_knows | |
"Yep only me!" | |
end | |
end | |
end | |
# One last one for your. Add directly to the eigen-"class" | |
def Book.oh_my | |
"(╯°□°)╯︵( .o.)" | |
end | |
p Book.catch_em_all | |
# => "I'm sorry, you can't do that Dave" | |
p Book.bind_it("(ノ◕ヮ◕)ノ*:・゚✧") | |
# => "We're still working on binding books with spell." | |
p Book.my_mind_hurts | |
# => "Stop gulping frozen water!" | |
#p Book.only_the_book_knows | |
# => NoMethodError: private method `only_the_book_knows' called for Book:Class | |
p Book.delegate_to_secret | |
# => "Yep only me!" | |
p Book.oh_my | |
# => "(╯°□°)╯︵( .o.)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment