Cheatography
https://cheatography.com
Syntax of Pharo Smalltalk
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Syntax element
startPoint |
a variable name |
Transcript |
a global variable |
self, super, nil, true, true, false, thisContext |
pseudo variable |
1 |
decimal integer |
2r101 |
binary integer |
1.5 |
floating point number |
2.4e7 |
number in exponential notation |
$a |
the character 'a' |
'Hello' |
the string 'Hello' |
#Hello |
the symbol #Hello |
#(1 2 3) |
a literal array |
{ 1 . 2 . 3 . 4 } |
a dynamic array |
"a comment" |
comment |
| x y | |
declaration of variable x and y |
x := 1 |
assign 1 to x |
[ :x | x + 2 ] |
a block that evaluates to x + 2 |
<primitive: 1> |
a method annotation |
3 factorial |
unary message factorial |
3 + 4 |
binary message + |
2 raiseTo: 6 modulo: 10 |
keyword message raiseTo:modulo: |
^ true |
return the value true |
Message precedence
Parentheses > Unary message > Binary message > Keyword message |
Cascade
OrderedCollection new
add: 1;
add: 2;
|
Mesages separated by semi-colon;
|
|
Conditionals
condition
ifTrue: [ block when condition is true ]
ifFalse: [ block when condition is false ]
|
Loops
(1 to: 10) do: [ :i | Transcript crShow: i ]. |
Loop with block execution |
arr collect: [ :i | i * 2 ]. |
Transform elements to get new collection |
arr select: [:i | i % 2 == 0 ]. |
Get only elements passed the test |
arr detect: [:i | i > 5 ] ifNone: []. |
Find the first element passed the test |
[ x < 10] whileTrue: [ x := x + 1]. |
Loop until test condition is passed |
Array
myArray at: 2 |
Get element by index |
myArray add: 'E1' |
Add element to array |
myArray at: 5 put: 'E1' |
Add element to index |
myArray remove: 'E1' |
Remove element at index |
myArray copyFrom: 1 to: 2 |
Subcollection |
myArray includes: 'E1' |
Test if element in array |
|