Created
April 14, 2011 05:13
-
-
Save janxious/918928 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 SayHello | |
def hello | |
puts "hello from SayHello" | |
end | |
end | |
include SayHello | |
hello |
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
Any of these should be runnable at the command line by ruby <filename> |
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
include Math | |
puts sqrt(25) |
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 Herp | |
def herp | |
"herp" | |
end | |
end | |
class Derp | |
include Herp | |
end | |
puts Derp.new.herp |
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
# you can use extend to add new class methods | |
module Home | |
def steal | |
"home" | |
end | |
end | |
class Score | |
extend Home | |
end | |
puts Score.steal |
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
# what's going on with extend | |
module Inspector | |
def saywhat | |
self.inspect | |
end | |
end | |
class Derp | |
include Inspector | |
extend Inspector | |
end | |
puts Derp.new.saywhat | |
puts Derp.saywhat |
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 M | |
CONSTANT = "value" | |
end | |
class C | |
include M | |
end | |
class D | |
extend M | |
end | |
puts C::CONSTANT | |
puts D::CONSTANT # wha? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment