Show Menu
Cheatography

Pharo Smalltalk syntax Cheat Sheet (DRAFT) by

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, thisCo­ntext
pseudo variable
1
decimal integer
2r101
binary integer
1.5
floating point number
2.4e7
number in expone­ntial 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 commen­t"
comment
| x y |
declar­ation of variable x and y
x := 1
assign 1 to x
[ :x | x + 2 ]
a block that evaluates to x + 2
<pr­imi­tive: 1>
a method annotation
3 factorial
unary message factorial
3 + 4
binary message +
2 raiseTo: 6 modulo: 10
keyword message raiseT­o:m­odulo:
^ true
return the value true

Message precedence

Parent­heses > Unary message > Binary message > Keyword message

Cascade

OrderedCollection new
  add: 1;
  add: 2;
Mesages separated by semi-c­olon;
 

Condit­ionals

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
Subcol­lection
myArray includes: 'E1'
Test if element in array