Squeak - Sizuha/devdog GitHub Wiki
Install
- Download: http://www.squeak.org/Downloads
๊ธฐ๋ณธ ์กฐ์
Workspace
- Command + D: do it. ๋ธ๋ญ์ผ๋ก ์ง์ ๋ ๋ถ๋ถ์ ์คํ.
- Command + P: print it. ๋ธ๋ญ์ผ๋ก ์ง์ ๋ ๋ถ๋ถ์ ์คํํ๊ณ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐ๋ก Workspace์ ํ์.
- Command + I: inspect it.
Transcript
Transcript ์๋์ฐ๋ TranscriptStream ํ์ ๊ฐ์ฒด๋ก ''Transcript''๋ผ๋ ์ ์ผํ ์ธ์คํด์ค๋ก ์ ๊ทผํ ์ ์๋ค.
Transcript show: 'Text Message'. "Transcript์ฐฝ์ ๋ฉ์ธ์ง๋ฅผ ํ์"
Transcript showln: 'Text Message'. "Transcript์ฐฝ์ ๋ฉ์ธ์ง๋ฅผ ํ์ํ๊ณ ๊ฐํ."
Transcript clear. "Transcript์ฐฝ์ ๋ด์ฉ์ ์ง์ด๋ค."
ํด๋์ค ์์ฑ
ํด๋์ค๋ฅผ ๋ง๋ ๋ค๋ ๊ฒ์, ์ด๋ค ์์ผ๋ก๋ ๋ชจ์ฒด๊ฐ ๋๋ ํด๋์ค๋ก๋ถํฐ ํ์ ํด๋์ค๋ฅผ ๋ง๋ ๋ค๋ ๊ฐ๋ .
๋ชจ์ฒด๊ฐ ๋๋ ํด๋์ค์ subclass ๋ฉ์ธ์ง๋ฅผ ๋ ๋ ค์ ์๋ก์ด ํ์ ํด๋์ค๋ฅผ ๋ง๋ค ์ ์๋ค.
"Integer ํด๋์ค์ ์ ์"
Number subclass: #Integer
instanceVariableNames: '' "์ธ์คํด์ค ๋ณ์"
classVariableNames: 'LowBitPerByteTable' "ํด๋์ค ๋ณ์"
poolDictionaries: ''
category: 'Kernel-Numbers'
๋ฉ์๋ ์์ฑ
System Browser์ UI๋ฅผ ์ด์ฉํด์ ์์ฑํ๋๊ฒ ๊ฐ๋จํ๋ค.
๋ฉ์๋ ๋ฆฌ์คํธ์ ๋น ๋ถ๋ถ์ ํด๋ฆญํ๋ฉด, ๋ฉ์๋ ๋ด์ฉ ํ์๊ฐ ๋ค์๊ณผ ๊ฐ์ด ๋์ค๋๋ฐ, ์ด ๋ด์ฉ์ ์์ ํ ํ์ Command + S ๋ก ์ ์ฅํ๋ฉด ๋๋ค.
messageSelectorAndArgumentNames
"comment stating purpose of message"
| temporary variable names |
statements
๋ฉ์๋ ์ ๊ฑฐ
Class removeSelector: #selector
์์ธ์ฒ๋ฆฌ
์๋ฌ๋ฐ์ ํ์ธ
[123 / 0] ifError: [ :err | Transcript clear; show: err. ].
์์ธ ํ์ธ
[ ... ] on: ์์ธํด๋์ค do: [ ์์ธ์ฒ๋ฆฌ ๋ด์ฉ. ]
[123 / 0] on: ZeroDivide do: [ Transcript clear; show: 'Catch Error: Zero Divide'. ].
์์ธ ๋์ง๊ธฐ
์์๋ก ์์ธ๋ฅผ ๋์ง๊ณ ์ ํ ๋๋, ํด๋น Exception class์ signal ๋ฉ์ธ์ง๋ฅผ ๋์ง๋ฉด ๋๋ค.
[ ^ZeroDivide signal ] on: ZeroDivide do: [ Transcript clear; show: 'Zero Divide Error !!' ]
์์
Dice
One Dice
- ๋ชฉํ: a ~ b ์ฌ์ด์ ์ซ์๊ฐ ๋๋คํ๊ฒ ๋์ค๋ ์ฃผ์ฌ์ ๊ฐ์ฒด๋ฅผ ๊ตฌํํ๋ค.
Dice ํด๋์ค๋ฅผ ๋ง๋ ๋ค. ๋จผ์ ๋ฒ ์ด์ค๊ฐ ๋ ํด๋์ค๊ฐ ํ์ํ๋ฐ, Random์ด ์ ๋นํ๊ฒ ๋ค.
Random subclass: #Dice
instanceVariableNames: 'min max'
classVariableNames: ''
poolDictionaries: ''
category: 'Kernel-Numbers'
์ด๊ธฐํ ๋งค์๋๋ฅผ ๋ง๋ค์.
"Instance Method. Category: private"
setMin: minValue max: maxValue
min := minValue.
max := maxValue.
"Instance Method. Category: accessing"
min
^min
max
^max
"class method. Category: instance creation"
newDice: minValue to: maxValue
| dice |
dice := super new.
dice setMin: minValue max: maxValue.
dice seed: Time totalSeconds.
^dice.
new
^self newDice: 1 to: 6
์ค์ ๋ก ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ๋ ๋งค์๋๋ฅผ ์ถ๊ฐ.
"Instance Method. Category: dice rolling"
roll
^ (self next * max) truncated + min
ํ ์คํธ.
dice := Dice new.
Transcript clear.
30 timesRepeat: [ Transcript show: dice roll; show: ', ' ].
Advanced Dice
๋ ๋ค์ํ ์ฃผ์ฌ์์ ํจํด์ ์ํด Interval ๊ฐ์ฒด๋ฅผ ์ด์ฉ.
ํด๋์ค ์ ์ ์์ .
Random subclass: #Dice
instanceVariableNames: 'range'
classVariableNames: ''
poolDictionaries: ''
category: 'Kernel-Numbers'
๊ธฐ์กด์ setMin:max: ๋์ ์ ๋ค์๊ณผ ๊ฐ์ด setRange: ๋งค์๋๋ฅผ ์์ฑํ๋ค.
setRange: anInterval
range := anInterval
๊ธฐ์กด์ min, max ๋งค์๋๋ ์ ๊ฑฐ.
๊ธฐ์กด์ newDice:to: ๋์ ์ ๋ค์๊ณผ ๊ฐ์ด new: ๋งค์๋๋ฅผ ์์ฑํ๋ค. new ๋งค์๋๋ ๊ฐ์ด ์์ ํ๋ค.
new: anInterval
| dice |
dice := super new.
dice setRange: anInterval.
dice seed: Time totalSeconds.
^dice.
new
^self new: (1 to: 6)
๊ธฐ์กด์ roll ๋งค์๋๋ฅผ ์์ ํ๋ค.
roll
^range at: (self nextInt: range size)
ํ ์คํธ.
dice := Dice new: (10 to: 100 by: 5).
Transcript clear.
30 timesRepeat: [ Transcript show: dice roll; show: ', ' ].
"Interval ๋ฟ๋ง ์๋๋ผ Array๋ก๋ ๊ฐ๋ฅ"
dice := Dice new: #(1 2 3 10 20 30).
Transcript clear.
30 timesRepeat: [ Transcript show: dice roll; show: ', ' ].