이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.
클래스 Array
에 메세지 new:
를 보내면 객체 Array
를 만들 수 있다.
Array
를 만들 때는 크기를 정해줘야 한다.
st> | anArray |
st> anArray := Array new: 10
(nil nil nil nil nil nil nil nil nil nil )
Array
를 생성하면 안의 원소들이 모두 nil
로 초기화 된다.
nil
은 스몰토크에서 '아무 것도 없음'을 나타내는 객체이다.
Array
에서 특정 위치에 있는 원소 하나에 접근하고 싶을 때는 메세지 at:
을 사용하면 된다.
st> anArray at:1
nil
Array
의 위치는 인덱스 index 라고 부른다.
스몰토크에서 인덱스는 숫자 1부터 시작한다.
Array
의 특정 위치에 객체를 저장하려면 메세지 at:put:
을 사용한다.
st> anArray at: 1 put: 'Toothbrush'
'Toothbrush'
객체 anArray
에 어떤 것들이 들어 있는지 보려면 객체 이름 anArray
를 입력하면 된다.
st> anArray
('Toothbrush' nil nil nil nil nil nil nil nil nil )
위와 같이 Array
의 첫번째 자리에 클래스 String
의 인스턴스인 'Toothbrush'
가 저장되어 있다.
이번에는 'Soap'
를 넣어보자.
st> anArray at: 2 put: 'Soap'
'Soap'
st> anArray
('Toothbrush' 'Soap' nil nil nil nil nil nil nil nil )
어떤 객체가 Array
안에 들어 있는지 보려면 메세지 include:
를 사용한다.
st> anArray includes: 'Soap'
true
st> anArray includes: 'Toothpaste'
false
Array
는 원소를 저장할 때 순서가 있는데 메세지 reverse
를 이용하면 순서를 뒤집을 수 있다.
st> anArray reverse
(nil nil nil nil nil nil nil nil 'Soap' 'Toothbrush' )
Set
은 저장하는 원소에 순서가 없고 원소의 개수도 제한이 없다.
메세지 new
를 이용하면 Set
인스턴스를 만들 수 있다. 인자는 필요 없다.
st> | aSet |
st> aSet := Set new
Set ()
처음에 Set
을 만들면 안에 아무 것도 들어있지 않다.
메세지 add:
를 이용하면 Set
안에 원소를 넣을 수 있다.
st> aSet add: 'Toothbrush'
'Toothbrush'
st> aSet
Set ('Toothbrush' )
이제 Set
안에는 문자열 객체 'Toothbrush'
가 들어있다.
문자열 객체를 하나 더 넣어보자.
st> aSet add: 'Soap'
'Soap'
st> aSet
Set ('Toothbrush' 'Soap' )
겉으로 보기에는 원소들에 순서가 있는 것 같지만 사실 순서는 없다.
문자열 객체를 하나 더 넣어보자.
st> aSet add: 'Toothpaste'
'Toothpaste'
st> aSet
Set ('Toothbrush' 'Soap' 'Toothpaste' )
여기서 'Toothbrush'
를 한 번 더 넣으면 어떻게 될까?
st> aSet add: 'Toothbrush'
'Toothbrush'
st> aSet
Set ('Toothbrush' 'Soap' 'Toothpaste' )
이미 Set
안에 들어있는 원소는 또 다시 추가되지 않는다.
Set
안에 들어있는 원소를 제거하려면 메세지 remove:
를 사용한다.
st> aSet remove: 'Toothpaste'
'Toothpaste'
st> aSet
Set ('Toothbrush' 'Soap' )
메세지 remove:
는 제거한 객체를 리턴한다.
remove:
를 사용한다고 해서 그 객체가 시스템에서 완전히 제거되는 것은 아니다. 그냥 해당 원소가 더 이상 Set
에 속하지 않게 되는 것 뿐이다.
st> | anElement |
st> anElement := 'Perfume'
'Perfume'
문자열 객체를 가리키는 별도 변수를 하나 만들었다. 이걸 Set
에 넣어보자.
st> aSet add: anElement
'Perfume'
st> aSet
Set ('Toothbrush' 'Soap' 'Perfume' )
그리고 이제 remove:
로 제거해보자.
st> aSet remove: anElement
'Perfume'
st> aSet
Set ('Toothbrush' 'Soap' )
이제 anElement
가 Set
에서 제거됐다. 아까 만든 변수가 아직 살아 있는지 보자.
st> anElement
'Perfume'
아직 살아 있다!
Dictionary
의 인덱스에는 숫자만 넣을 수 있는 것은 아니고 어떤 객체라도 넣을 수 있다.
Dictionary
는 Set
과 마찬가지로 원소들에 순서가 없고 만들 때 길이를 정해주지 않아도 된다.
메세지 new
로 Dictionary
를 만들 수 있다.
st> | aDictionary |
st> aDictionary := Dictionary new
Dictionary (
)
처음 Dictionary
를 만들면 안에 아무 것도 들어있지 않다.
메세지 at:put:
을 이용해서 딕셔너리에 원소를 추가할 수 있다.
st> aDictionary at: 'Canol' put: 'Gokel'
'Gokel'
st> aDictionary
Dictionary (
'Canol'->'Gokel'
)
문자열 객체를 하나 더 넣어보자.
st> aDictionary at: 'Paolo' put: 'Bonzini'
'Bonzini'
st> aDictionary
Dictionary (
'Canol'->'Gokel'
'Paolo'->'Bonzini'
)
메세지 keys
를 이용하면 딕셔너리 내에 모든 키를 Set
객체로 리턴한다.
st> aDictionary keys
Set ('Canol' 'Paolo' )
removeKey
를 이용하면 딕셔너리에 있는 원소를 제거할 수 있다.
st> aDictionary removeKey: 'Canol'
'Gokel'
st> aDictionary
Dictionary (
'Paolo'->'Bonzini'
)