Created
November 18, 2009 20:31
-
-
Save ender672/238217 to your computer and use it in GitHub Desktop.
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
module Sliceable | |
def slice! | |
if @sliced | |
puts "The #{name} is already sliced!\n" | |
else | |
@sliced = true | |
puts "Done slicing the #{name}.\n" | |
end | |
end | |
end | |
# The singleton module allows us to create singleton classes. | |
require 'singleton' | |
class Apple | |
include Singleton | |
extend Sliceable | |
end | |
# A Ruby module makes a more lightweight singleton. | |
module Persimmon | |
extend Sliceable | |
end | |
# An object instance can serve as a super lightweight singleton. | |
Tangelo = Object.new | |
def Tangelo.name; "Tangelo"; end | |
Tangelo.extend Sliceable | |
# Singleton classes allow inheritance. The other alternatives don't allow this. | |
class Pair < Apple | |
end | |
[Apple, Persimmon, Tangelo, Pair].each do |c| | |
2.times{ c.slice! } | |
begin | |
c.new | |
rescue NoMethodError | |
puts("Unable to call #{c.name}.new\n\n") | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment