public
Last active

  • Download Gist
00-simple.rb
Ruby
1 2 3 4 5 6 7 8 9
module SayHello
def hello
puts "hello from SayHello"
end
end
 
include SayHello
 
hello
000-README
1
Any of these should be runnable at the command line by ruby <filename>
01-existing-module.rb
Ruby
1 2 3
include Math
 
puts sqrt(25)
02-classy.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11
module Herp
def herp
"herp"
end
end
 
class Derp
include Herp
end
 
puts Derp.new.herp
03-classlevel.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
# you can use extend to add new class methods
module Home
def steal
"home"
end
end
 
class Score
extend Home
end
 
puts Score.steal
04-classlevel2.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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
05-constants.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
module M
CONSTANT = "value"
end
 
class C
include M
end
 
class D
extend M
end
 
puts C::CONSTANT
puts D::CONSTANT # wha?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.