Created
August 19, 2011 15:20
-
-
Save nevans/1157046 to your computer and use it in GitHub Desktop.
My favorite module methods pattern, and how it works with private
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
#!/usr/bin/env ruby | |
module M | |
extend self | |
def foo | |
"hello world" | |
end | |
private | |
def bar | |
"private!" | |
end | |
end | |
class C | |
include M | |
def baz | |
"calling bar: #{bar}" | |
end | |
end | |
puts M.foo | |
puts C.new.foo | |
puts C.new.baz | |
begin; puts M.bar; rescue => ex; puts "Exception! => #{ex}"; end | |
begin; puts C.new.bar; rescue => ex; puts "Exception! => #{ex}"; end |
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
hello world | |
hello world | |
calling bar: private! | |
Exception! => private method `bar' called for M:Module | |
Exception! => private method `bar' called for #<C:0x2078f80> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment