Last active
August 29, 2015 14:10
-
-
Save drhuffman12/22ca54dbaf4074de459e to your computer and use it in GitHub Desktop.
Simple Object-oriented example in Ruby
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
# Official site for the Ruby language: "https://www.ruby-lang.org/" | |
class C1 | |
attr :a, :b | |
def initialize(a) | |
@a = a | |
end | |
def self.hi | |
puts 'hello world' | |
end | |
def hi | |
puts 'hi there' | |
end | |
end | |
module M1 | |
def foo | |
puts self.class | |
return 123 | |
end | |
end | |
class C2 < C1 | |
include M1 | |
def initialize(a,b) | |
super(a) | |
@b = b | |
end | |
end | |
module M2 | |
def b | |
@b=8 | |
end | |
def bar(a) | |
'bar '*a | |
end | |
end | |
class C3 < C1 | |
include M1 | |
include M2 | |
def widget | |
bar(@a) | |
end | |
end | |
C1.hi | |
puts | |
o1 = C1.new(1) | |
o1.hi | |
puts "o1.a == #{o1.a}" | |
puts "o1.b == #{o1.b}" | |
puts "o1.b.nil? == #{o1.b.nil?}" | |
puts | |
o2 = C2.new(2,7) | |
puts "o2.a == #{o2.a}" | |
puts "o2.b == #{o2.b}" | |
puts "o2.b.nil? == #{o2.b.nil?}" | |
puts "o2.foo == #{o2.foo}" | |
puts | |
o3 = C3.new(3) | |
puts "o3.a == #{o3.a}" | |
puts "o3.b == #{o3.b}" | |
puts "o3.b.nil? == #{o3.b.nil?}" | |
puts "o3.foo == #{o3.foo}" | |
puts "o3.bar(30) == #{o3.bar(30)}" | |
puts "o3.widget == #{o3.widget}" | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment