Pharo FAQ

Installation

Q Where do I get the latest Pharo?
A Go to http://pharo-project.org/download or http://gforge.inria.fr/frs/?group_id=1299
Q How do I load the latest Pharo updates?
A Utilities → updateFromServer

Alternatively, you can evaluate:

Utilities updateFromServer

The following is also handy if you want to upgrade only up to a specific version:

Utilities updateFromServerThroughUpdateNumber: 10300
Q How do I update OmniBrowser?
A ScriptLoader loadOB


Environment

Q How do I register the browser that I want to be the default?
A Use the menu in the top left of the Browser window.
Q How do I turn off/on smart quotes and auto-completion?
A world menu->open...Preference Browser. Got to "developer image" category and disable "ecompletionSmartCharacters"
Q How do I use OCompletion?
A Installer squeaksource project: 'OCompletion'; install: 'OCLoader'

If OmniBrowser is not recognizing it, just evaluate OCompletionTable lookForOB.

There is also a paper explaining how it works and why it is better than ECompletion.

Q How do I use auto-completion?
A ctrl-space
Q How do I programmatically set preferences?
A (Preferences dictionaryOfPreferences at: #showDeprecationWarnings) preferenceValue: true.
Q How do I programmatically open a workspace?
A
SHWorkspace new
	contents: 'contents';
	openLabel: 'TASKS'.


Packages

Q How do I create a SqueakSource project?
A
  • go to squeaksource.com
  • register yourself as a new member
  • register a project (name = category)
  • copy the Registration code snippet
  • open>Monticello browser
  • +Package to add the category
  • Select the package
  • +Repository>HTTP
  • paste and accept the Registration code snippet; enter your password
  • Save to save the first version
Q How do I load a Squeaksource project?
A
  • Find the project in squeaksource
  • Copy the Registration code snippet
  • open>Monticello browser
  • +Repository>HTTP
  • paste and accept the Registration code snippet; enter your password
  • select the new repository and Open it
  • Select and load the latest version
Q How do I programmatically load projects from SqueakSource?
A
| mc fileToLoad version |
mc := Smalltalk at: #MCHttpRepository
	ifPresent: [:repoClass |
		repoClass location: 'www.squeaksource.com/Installer'
			user: 'squeak' password: 'squeak'].
fileToLoad := mc readableFileNames
		detect: [ :aFile | aFile beginsWith:'Installer-sd.3' ]
		ifNone: [ nil ].
version := mc versionFromFileNamed: fileToLoad.
version workingCopy repositoryGroup addRepository: mc.
mc creationTemplate: mc asCreationTemplate.
version load.

NB: Installer provides a high-level interface to specify load scripts.

Q What do I do when the Installer complains that the Universe code is not there?
A Run: ScriptLoader new installingUniverse
Q How do I define extension methods?
A Suppose you want to extend Number with Number>>chf but have Monticello recognize it as being part of my Money package.

Put it in a method-category named *money. MC gathers all extension methods that are in categories named like *package.

Q How do I run Code critics on a package?
A Start a pharo-dev image with the latest refactoring Browser code in it.
  • Select your package in the OB class or package browser.
  • Select open environment > package [opens a package browser]
  • Select open > Code Critics

If you directly open code critics, it will analyze the whole smalltalk system!

Q How do I move all classes and extension methods from one package to another?
A
| pkg |
pkg := PackageInfo named: 'Saphir'.
pkg classes do: [:class | class category: 'Coral' ].
pkg methods collect: [:method |
	method actualClass organization
		classify: method methodSymbol
		under: #'*coral' ]


Browsing

Q How do I find/browse all sends to super?
A Two alternatives:
SystemNavigation default browseAllSelect: [:method | method sendsToSuper ]
SystemNavigation default browseMethodsWithSourceString: 'super'
Q How do I browse all super sends within a hierarchy?
A The old way:
class := Collection.
SystemNavigation default
  browseMessageList: (class withAllSubclasses gather: [ :each |
    each methodDict associations
      select: [ :assoc | assoc value sendsToSuper ]
      thenCollect: [ :assoc | MethodReference class: each selector: assoc key ] ])
  name: 'Supersends of ' , class name , ' and its subclasses'

The refactoring browser way:

((BrowserEnvironment new forClasses: (Collection withAllSubclasses))
  selectMethods: [:method | method sendsToSuper])
  open
Q How do I find out which are the new methods implemented in a class (i.e., not overridden or extended?
A Example: Integer
[:aClass| aClass methodDict keys select: [:aMethod |
	(aClass superclass canUnderstand: aMethod) not ]] value: Integer
Q How do I tell which methods are abstract?
A
[:aClass| aClass methodDict keys select: [:aMethod |
	(aClass>>aMethod) isAbstract ]] value: Number
Q How do I find all the Traits in the system?
A Smalltalk allTraits
Q How do I find which classes use traits?
A Smalltalk allClasses select: [:each | each hasTraitComposition ]
Q How do I browse messages sent but not implemented?
A SystemNavigation default browseAllUnimplementedCalls

Also Smalllint: open > code critics on a category will do it.


Testing

Q How do I programmatically run tests of specific categories?
A
| tr |
tr := TestRunner new.
ToolBuilder open: tr.
tr
	categoryAt: (tr categoryList indexOf: 'SCGPier') put: true; "etc"
	selectAllClasses;
	runAll.
Q How do I check which methods of my package are not covered by tests?
A The Pharo SUnit Test Runner has a "Run Coverage button.

If you have an older image, just evaluate Installer ss project: 'Pharo'; install: 'SUnitGUI'.

The test runner will normally prompt you for the package to check for coverage. To eliminate this prompt, you can explicitly declare the package(s) to be covered on the class side of your test class as follows:

packageNamesUnderTest
	^ #('SplitJoin')

To indicate that a particular method should not be considered in the coverage check, simply add the annotation: <ignoreForCoverage> To exclude whole classes within a package from consideration for coverage, define a class side method on your test class called classNamesNotUnderTest:

classNamesNotUnderTest
	^ #('GUI' 'README')


Streams and Collection

Q How do I convert a collection of characters to a String? E.g. 'hello world' asSet ...
A (WriteStream with: String new) nextPutAll: 'hello world' asSet; contents


Exceptions

Q How do I catch exceptions?
A [ ... ] on: Error do: [ ... ]


Instrumentation

Q How do I time a block?
A Time millisecondsToRun: aBlock
Q How do I profile code?
A TimeProfileBrowser spyOn: [ ... ].
Q How do I introduce a delay?
A (Delay forSeconds: 5) wait.
Q How do I load method wrappers?
A Go to http://www.squeaksource.com/MethodWrappers.html

Be sure to load MethodWrappers3.9 for the 3.9 squeak image.

An alternative is to use http://www.squeaksource.com/ObjectsAsMethodsWrap.html which implements run:with:in:# instead of subclassing CompiledMethod.


Images

Q How do I save an image file as a constant method?
A Just drop the image file onto the Pharo image, inspect it, and print 'self form storeString'.

The result will (should) be usable to recreate the image. For example (self here is the ByteString):

(SketchMorph withForm: (Compiler evaluate: self)) openInWorld
Q How do I load an image file?
A (SketchMorph fromFile: 'sq.png') openInWorld
Q How do I refresh a Morph? (i.e., to animate an algorithm)
A Send self layoutChanged

Does not seem to work ...


Files

Q How do I read from or write to a file?
A
(FileDirectory default newFileNamed: 'tmp.txt') nextPutAll: 'stuff'; close.
(FileDirectory default oldFileNamed: 'tmp.txt') contents.
Q How do I delete a file?
A FileDirectory default deleteFileNamed: ’testAlpha32.png’
Q How do I prompt the user to select a file or directory?
A FileList2 modalFolderSelector


Parsing

Q Which packages should I load from http://www.squeaksource.com/SmaccDevelopment.html to write parsers?
A Load AST, Refactoring-Core, SmaCC and SmaCCDev
ScriptLoader loadLatestPackage: 'AST' from: 'http://www.squeaksource.com/AST'.
ScriptLoader loadLatestPackage: 'Refactoring-Core' from: 'http://www.squeaksource.com/RefactoringEngine'.
ScriptLoader loadLatestPackage: 'SmaCC' from: 'http://www.squeaksource.com/SmaccDevelopment'.
ScriptLoader loadLatestPackage: 'SmaCCDev' from: 'http://www.squeaksource.com/SmaccDevelopment'.

Be sure that SmaCC is up-to-date first! NB: The first two are already in the Pharo-dev images Warning: SmaCC-Development is for Squeak 3.8 — don’t use it

Q How do I run SmaCC?
A Just load SmaCCDev and it will start up — it will also be loaded into the menu.
Q SmaCC pops up an annoying "silence not found in the Sound library" when I click on the tabs — how do I get it to stop?
A Edit SampledSound class>>soundNamed: and comment out the self inform:
Q How do I generate a view of the AST of an expression?
A (RBParser parseExpression: '3+4') explore [explore it]


Regular Expressions

Q Where are regexes documented?
A VB-Regex RxParser class DOCUMENTATION
Q How can I ask if a regex matches any part of string?
A Use re search: aString instead of re matches: aString


Miscellaneous

Q How do I report an error message to the user?
A self inform: "..."
Q How do I run a headless image with a file argument?
A Can’t do it with the MacOSX VM


Last changed by admin on 21 April 2009