forked from rabajaj0509/Ruby-concepts-step-by-step
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter3-Modules and Program Organization
- Loading branch information
1 parent
77337e3
commit 17ce245
Showing
5 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
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
21
Chapter3-Modules and Program Organization/stack_program/stack.rb
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
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 | ||
|
13 changes: 13 additions & 0 deletions
13
Chapter3-Modules and Program Organization/stack_program/stacklike.rb
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
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 |
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
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 |