Skip to content

Commit

Permalink
Chapter3-Modules and Program Organization
Browse files Browse the repository at this point in the history
  • Loading branch information
rabajaj0509 committed Aug 5, 2017
1 parent 77337e3 commit 17ce245
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Chapter3-Modules and Program Organization/mix-ins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Modules dont have instances, instead modules get mixed-in
#to classes, using the include method

#The result of mixing in a module is that instances of the class have
#acces to the instance methods defined in the module.


module MyFirstModule
def say_hello
puts "Hello"
end
end

class ModuleTester
include MyFirstModule
end

mt = ModuleTester.new
mt.say_hello
54 changes: 54 additions & 0 deletions Chapter3-Modules and Program Organization/stack_program/\
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import os
import pdb

def man():
print ("========= help page ==========")
print ("python3 todo.py [OPTION]...")
print ("\t -a \"TASK\" \n\t\t Add task")
print ("\t -v \n\t\t List all task")
print ("\t -s \n\t\t Seek task")
print ("\n========= To DO App ==========")
sys.exit(1)
############ Append Function #############
def add():
if len(sys.argv) != 3:
print ("Add task")
print ("\t e.g - python3 todo.py -a \"TASK\"")
else:
task = sys.argv[2]
if os.path.isfile("./task.txt"):
f=open("./task.txt","a")
f.write(task + "\n")
f.close()
print ("Task added")
else:
f=open("./task.txt","w")
f.write(task + "\n")
f.close()
print ("Task added")
############ Display Function #############
def display():
if os.path.isfile("task.txt"):
f=open("task.txt","r")
print (f.read())
f.close()
else:
print ("File not exist \t Please add task first")

###### Main Function #########
def main():
if len(sys.argv) <= 1:
man()
elif len(sys.argv) >= 1:
if (sys.argv[1]=="-a"):
add()
elif (sys.argv[1]=="-v"):
display()
elif (sys.argv[1]=="-d"):
pass
elif (sys.argv[1]=="-u"):
pass

main()

21 changes: 21 additions & 0 deletions Chapter3-Modules and Program Organization/stack_program/stack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative "stacklike"

class Stack
include Stacklike
end

s = Stack.new
s.add_to_stack("item one")
s.add_to_stack("item two")
s.add_to_stack("item three")

puts "\nObjects currently on stack"
puts s.stack

taken = s.take_from_stack
puts "\nRemoved this object: "
puts taken

puts"\nNow on stack"
puts s.stack

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Stacklike
def stack
@stack ||= []
end

def add_to_stack(obj)
stack.push(obj)
end

def take_from_stack
stack.pop
end
end
18 changes: 18 additions & 0 deletions Chapter3-Modules and Program Organization/super.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Maintain
def report
puts "'report' method in module Maintain"
end
end

class Contains
include Maintain
def report
puts "'report' method in class Contains"
puts "About to trigger the next higher-up report method..."
super
puts "Back from the 'super' call"
end
end

c = Contains.new
c.report

0 comments on commit 17ce245

Please sign in to comment.