Skip to content

Instantly share code, notes, and snippets.

@ender672
Created November 18, 2009 20:31
Show Gist options
  • Save ender672/238217 to your computer and use it in GitHub Desktop.
Save ender672/238217 to your computer and use it in GitHub Desktop.
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