Created
March 31, 2011 09:15
-
-
Save jlebrech/896090 to your computer and use it in GitHub Desktop.
freezing.
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 Freeza # you can define a class once | |
def hello | |
puts "hello" | |
end | |
end | |
f = Freeza.new | |
f.hello | |
class Freeza # twice | |
def goodbye | |
puts "goodbye" | |
end | |
end | |
f.goodbye | |
Freeza.freeze # freeze the lady | |
begin | |
class Freeza | |
def hello | |
puts "is it me you're looking for" | |
end | |
end # this will give out an error, as the class is frozen | |
rescue RuntimeError | |
puts "Get back to where you once belonged" | |
end | |
# what you have to do is inherit from that class and change variables to be of the class | |
class Defrosted < Freeza | |
def hellohello | |
puts "hello hello" | |
end | |
end | |
f = Defrosted.new | |
# thats all good, but you might want to keep the state between objects | |
# so the values from the old class need copying between them. | |
f.hellohello # not very flexible, as you are just redefining a class | |
# lets make a module instead | |
module MoCowBell | |
def donk | |
puts "donk" | |
end | |
end | |
# i want the variable f to make cowbell noises | |
f.extend MoCowBell | |
f.donk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment