This file contains hidden or 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
class Counter: | |
count = 0 # class attribute count | |
def increment(): # define method to increment count | |
count += 1 | |
Counter.count # count is 0 | |
Counter.increment() # call the method increment() | |
Counter.count # count is 1 | |
counter = Counter() # create instance of Counter | |
counter.count # class attribute... count is still 1 |
This file contains hidden or 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
{ | |
"Fruit": | |
[ | |
{ | |
"Name": "Banana", | |
"Colour": "Yellow" | |
}, | |
{ | |
"Name": "Apple", | |
"Colour": "Green" |
This file contains hidden or 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
switch (expression) | |
{ | |
case value1: | |
// code executed if expression matches value1 | |
[break;] | |
case value2: | |
// code executed if expression matches value2 | |
[break;] | |
... | |
case valueN: |
This file contains hidden or 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
switch (colour) | |
{ | |
case 'red': | |
case 'amber': | |
stop(); | |
break; | |
case 'green': | |
case 'flashing amber': | |
go(); | |
break; |
This file contains hidden or 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
const stop = () => { console.log("stop called"); } | |
const go = () => { console.log("go called"); } | |
const colour = 'red'; | |
const handleColour = | |
{ | |
'red' : stop, | |
'amber' : stop, | |
'green' : go, |
This file contains hidden or 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
const handleShape = (shape) => { console.log(shape.area()); } | |
class Shape | |
{ | |
constructor(width = 2) | |
{ | |
this.width = width; | |
} | |
area() | |
{ |
This file contains hidden or 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
var item; | |
try | |
{ | |
item = previousItem; | |
} | |
catch (ReferenceError) | |
{ | |
item = new Item(); | |
previousItem = item; |
This file contains hidden or 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
def print_balance(account) | |
printf "Debits: %10.2f\n", account.debits | |
printf "Credits: %10.2f\n", account.credits | |
if account.fees < 0 | |
printf "Fees: %10.2f-\n", -account.fees | |
else | |
printf "Fees: %10.2f\n", account.fees | |
end | |
printf " ———-\n" | |
if account.balance < 0 |
This file contains hidden or 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
def format_amount(value) | |
result = sprintf("%10.2f", value.abs) | |
if value < 0 | |
result + "-" | |
else | |
result + " " | |
end | |
end | |
def print_line(label, value) |
This file contains hidden or 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
## Options section | |
setopt correct # Auto correct mistakes | |
setopt extendedglob # Extended globbing. Allows using regular expressions with * | |
setopt nocaseglob # Case insensitive globbing | |
setopt rcexpandparam # Array expension with parameters | |
setopt nocheckjobs # Don't warn about running processes when exiting | |
setopt numericglobsort # Sort filenames numerically when it makes sense | |
setopt nobeep # No beep | |
setopt appendhistory # Immediately append history instead of overwriting |