Skip to content

Instantly share code, notes, and snippets.

@nevans
Created August 19, 2011 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevans/1157046 to your computer and use it in GitHub Desktop.
Save nevans/1157046 to your computer and use it in GitHub Desktop.
My favorite module methods pattern, and how it works with private
#!/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
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