diff --git a/Chapter3-Modules and Program Organization/mix-ins.rb b/Chapter3-Modules and Program Organization/mix-ins.rb new file mode 100644 index 0000000..530d838 --- /dev/null +++ b/Chapter3-Modules and Program Organization/mix-ins.rb @@ -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 diff --git "a/Chapter3-Modules and Program Organization/stack_program/\\" "b/Chapter3-Modules and Program Organization/stack_program/\\" new file mode 100644 index 0000000..9cee028 --- /dev/null +++ "b/Chapter3-Modules and Program Organization/stack_program/\\" @@ -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() + diff --git a/Chapter3-Modules and Program Organization/stack_program/stack.rb b/Chapter3-Modules and Program Organization/stack_program/stack.rb new file mode 100644 index 0000000..34bcce2 --- /dev/null +++ b/Chapter3-Modules and Program Organization/stack_program/stack.rb @@ -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 + diff --git a/Chapter3-Modules and Program Organization/stack_program/stacklike.rb b/Chapter3-Modules and Program Organization/stack_program/stacklike.rb new file mode 100644 index 0000000..8f0c04f --- /dev/null +++ b/Chapter3-Modules and Program Organization/stack_program/stacklike.rb @@ -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 diff --git a/Chapter3-Modules and Program Organization/super.rb b/Chapter3-Modules and Program Organization/super.rb new file mode 100644 index 0000000..80b76cc --- /dev/null +++ b/Chapter3-Modules and Program Organization/super.rb @@ -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