Skip to content

Instantly share code, notes, and snippets.

@fjolnir
Created June 21, 2012 15:01
Show Gist options
  • Save fjolnir/2966260 to your computer and use it in GitHub Desktop.
Save fjolnir/2966260 to your computer and use it in GitHub Desktop.
\ A backslash outside a string is a comment
¥ The Yen sign can also be used (See Japanese keyboard layouts to understand why)
\ Keywords
yes
no
nil
self
super
\ Variable assignment
a = b \ Variables are local in scope
\ Variables must begin with a lowercase letter, as uppercase names
\ are reserved for classes
\ Arrays & Dictionaries
anArray = #[ a, b, c ] \ Initialises an array containing 3 elements
aDict = #{ key = value, anotherKey = value } \ Initializes a dictionary
\ Blocks
aBlock = { ..body.. } \ A block. Defines scope
aBlockWith = { arg0, arg1 | ..body.. } \ A block that takes two arguments
aBlock = { &arg | ..body.. } \ Prefixing an argument with & indicates that rather than evaluating it,
\ a block with it as it's body should be passed
aBlock = { arg=123 | ..body.. } \ Assignment in the argument list indicates a default value
\ for that argument
\ Block calls
aBlock() \ Calls a block with no arguments
aBlock(something, somethingElse) \ Calls a block with a two arguments
\ Objects
#Klass < Object {
+ new { \ Class method
self = super new. \ Calls superclass method
self#ivar = 123 \ Sets instance variable
return: self.
}
- aMethod: a and: b { \ Instance method taking two arguments
#ivar = a + b \ Shorthand for self#ivar
}
}
\ Passing messages to objects
instance = Klass new
instance aMethod: 123 and: 456
instance aMethod: 123. \ To explicitly terminate a message you use a period
\ Accessing member variables
obj#member = 123
a = obj#member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment