### Swazoo Configuration File Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/Swazoo An example of a Swazoo server configuration file. It defines a site named 'hello' listening on port 8080 and a 'HelloWorldResource' for the 'hello.html' path. ```smalltalk ``` -------------------------------- ### Load Packages using gst-load Script Source: https://www.gnu.org/software/smalltalk/manual/html_node/Packages Illustrates the command-line usage of the `gst-load` script to install packages and their dependencies. This script automates the process of loading specified packages and any necessary prerequisites, then optionally saves the image and exits. It's useful for automated deployments or initial setup. ```bash gst-load DBD-MySQL DBD-SQLite DBI ``` -------------------------------- ### Example Account Transactions in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Moving-money-around Demonstrates how to use the 'deposit:' and 'spend:' methods on an account object in Smalltalk. These examples show basic financial operations like depositing and spending money. ```smalltalk a deposit: 125 a deposit: 20 a spend: 10 ``` -------------------------------- ### GNU Smalltalk: Example Method Definition Source: https://www.gnu.org/software/smalltalk/manual/html_node/Syntax Provides a concrete example of defining methods (radiusToArea, radiusToCircumference) for the Number class. ```smalltalk Number extend [ radiusToArea [ ^self squared * Float pi ] radiusToCircumference [ ^self * 2 * Float pi ] ] ``` -------------------------------- ### Start Swazoo Web Server using gst-load Source: https://www.gnu.org/software/smalltalk/manual/html_node/Swazoo Starts the Swazoo web server using the `gst-load` command. The `[=ARG]_` placeholder typically represents 'swazoodemo' for a demo servlet or a path to a configuration file. ```shell $ gst-load --start_[=ARG]_ Swazoo ``` -------------------------------- ### Start Swazoo Web Server using gst-remote Source: https://www.gnu.org/software/smalltalk/manual/html_node/Swazoo Loads and starts the Swazoo web server into a background GNU Smalltalk virtual machine using `gst-remote`. The `_[:ARG]_` placeholder can be 'swazoodemo' or a configuration file path. ```shell $ gst-remote --start=Swazoo_[:ARG]_ ``` -------------------------------- ### Smalltalk Binary Expression Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Demonstrates a typical Smalltalk binary expression involving arithmetic operators. The example shows how a sequence of binary operations is parsed according to operator precedence. ```smalltalk 1 + 2 - 3 / 4 ``` ```smalltalk (((1 + 2) - 3) / 4) ``` -------------------------------- ### Prepare Source Tree using gst-package Source: https://www.gnu.org/software/smalltalk/manual/html_node/Packages Creates a skeleton GNU style source tree for a package. This includes generating a configure.ac script to find the GNU Smalltalk installation path and a Makefile.am to support standard Makefile targets like `make install` and `make dist`. This command is run from the directory intended to be the top of the source tree. ```shell gst-package --prepare path1/package.xml path2/package.xml ``` -------------------------------- ### GNU Smalltalk: Class Pragmas Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/Syntax Provides examples of pragmas used in class definitions for comments, categories, imported namespaces, and instance variable shapes. ```smalltalk ``` -------------------------------- ### Start Seaside Server in Foreground (Shell) Source: https://www.gnu.org/software/smalltalk/manual/html_node/Seaside Command to start the Seaside web server in the foreground within a GNU Smalltalk image. The server will be accessible at http://localhost:8080/seaside. ```shell $ gst-load -I seaside.im --start Seaside ``` -------------------------------- ### Smalltalk Compile-Time Evaluation Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Provides an example of the 'eval' syntax in Smalltalk, which allows arbitrary expressions to be evaluated at compile-time. ```smalltalk ##(Object allInstances size) ``` -------------------------------- ### Smalltalk Symbol Syntax Examples Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Shows different forms of symbol literals in Smalltalk, including simple identifiers, binary selectors, and keyword selectors. ```smalltalk #hello ``` ```smalltalk #+ ``` ```smalltalk #at:put: ``` -------------------------------- ### Smalltalk: Enable Verbose Mode Source: https://www.gnu.org/software/smalltalk/manual/html_node/Saying-hello This command shows how to start the GNU Smalltalk interpreter in verbose mode by passing the `-V` flag. Verbose mode provides detailed statistics about the performance of the Smalltalk engine. ```bash $ gst -V ``` -------------------------------- ### Install Package using gst-package Source: https://www.gnu.org/software/smalltalk/manual/html_node/Packages Installs a GNU Smalltalk package by creating a .star archive. It can take a local package.xml file, a local .star file, or a URL to a package.xml or .star file. When downloading remote files, it performs checks for subpackages in the same repository. ```shell gst-package path/to/package.xml ``` ```shell gst-package http://smalltalk.gnu.org/project/Iliad/package.xml ``` ```shell gst-package --download Iliad ``` -------------------------------- ### Smalltalk Variable Binding Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Demonstrates the syntax for creating a variable binding literal in Smalltalk, used to reference a class, potentially with namespace support. ```smalltalk #{Class} value ``` ```smalltalk #{Smalltalk.Class} ``` -------------------------------- ### Smalltalk Integer Arithmetic Examples Source: https://www.gnu.org/software/smalltalk/manual/html_node/Math-in-Smalltalk Illustrative examples of arithmetic expressions using integers and operators such as multiplication, division, addition, and subtraction in Smalltalk. These examples showcase message passing for mathematical computations. ```Smalltalk 8 * (4 / 2) 8 - (4 + 1) 5 + 4 2/3 + 7 2 + 3 * 4 2 + (3 * 4) ``` -------------------------------- ### Defining C Function Arguments for 'open(2)' Source: https://www.gnu.org/software/smalltalk/manual/html_node/C-callout Provides an example of how to specify the argument types for a C function call using the `args:` keyword in the `` syntax. This specific example is for the `open(2)` C function. ```Smalltalk args: #(#string #int #int) ``` -------------------------------- ### Smalltalk 'Hello, world' Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/What-happened This Smalltalk code creates a String object and sends it the 'printNl' message, which results in the string being printed to the console. The code itself is simple, relying on the String object's methods to handle the printing process. ```smalltalk 'Hello, world' printNl ``` -------------------------------- ### Start Seaside Server in Background (Shell) Source: https://www.gnu.org/software/smalltalk/manual/html_node/Seaside Command to start the Seaside web server as a background daemon. This allows for remote control and management of the virtual machine. The server will be accessible at http://localhost:8080/seaside. ```shell $ gst-remote -I seaside.im --daemon --start=Seaside ``` -------------------------------- ### Saving Smalltalk Session State with Snapshot Source: https://www.gnu.org/software/smalltalk/manual/html_node/Creating-classes Saves the current state of the GNU Smalltalk environment, including all variables, classes, and definitions, into an image file. This allows users to resume their session later without retyping previous examples. The snapshot file can then be used to restart Smalltalk from that saved state. ```Smalltalk ObjectMemory snapshot: 'myimage.im' ``` -------------------------------- ### Smalltalk Global Object Reference Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/Namespaces Demonstrates referencing a global object 'John' within an environment. This requires an association where 'John' maps to 'aMan' in the current or a super-environment. ```Smalltalk John goHome ``` -------------------------------- ### Basic GNU Smalltalk Invocation Source: https://www.gnu.org/software/smalltalk/manual/html_node/Invocation The fundamental command to start the GNU Smalltalk virtual machine. It can accept flags and files as arguments. The interpreter will automatically ensure the binary image file (gst.im) is up-to-date. ```shell gst [ flags … ] [ file … ] ``` -------------------------------- ### Example of Invoking a C Function from Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/C-callout Demonstrates how to call a C function, previously defined using the cCall mechanism, from within Smalltalk. This example shows invoking the 'system' C function. ```Smalltalk Smalltalk system: 'lpr README' ! ``` -------------------------------- ### Smalltalk Array Constant Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Demonstrates the creation of an array constant in Smalltalk, which can include numbers, strings, characters, symbols, and hexadecimal values. ```smalltalk a := #(1 2 'Hi' $x #Hello 4 16r3F) ``` -------------------------------- ### Smalltalk Message Precedence Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Illustrates the default precedence of message sends in Smalltalk, showing how parentheses can be used to explicitly group operations, especially for keyword and binary messages. ```smalltalk myvar at: 2 + 3 put: 4 mybool ifTrue: [ ^ 2 / 4 roundup ] ``` ```smalltalk (myvar at: (2 + 3) put: (4)) (mybool ifTrue: ([ ^ (2 / (4 roundup)) ])) ``` -------------------------------- ### Smalltalk Common Optimized Patterns Source: https://www.gnu.org/software/smalltalk/manual/html_node/Performance Shows examples of code constructs that are frequently optimized by GNU Smalltalk's compiler and virtual machine, such as the combination of boolean evaluations with jumps and the optimized execution of loops and conditionals. ```Smalltalk 1 to: 5 do: [ :i | … ] a < b and: [ … ] myObject isNil ifTrue: [ … ] ``` -------------------------------- ### Interactive GNU Smalltalk Session Source: https://www.gnu.org/software/smalltalk/manual/html_node/Invocation An example of what the console output looks like when GNU Smalltalk is invoked without specifying files, indicating it's ready for interactive input with a 'st>' prompt. ```shell "Global garbage collection... done" GNU Smalltalk ready st> ``` -------------------------------- ### Smalltalk NiledArray Example: Inheritance Source: https://www.gnu.org/software/smalltalk/manual/html_node/Inheritance-and-Polymorphism Demonstrates inheritance in Smalltalk using the NiledArray class. It shows how a subclass can utilize existing methods from its superclass (Array) with minimal additional code. This example highlights the power of inheritance for code reuse and incremental development. ```smalltalk a := NiledArray new: 10 a at: 5 put: 1234 a do: [:i| i printNl ] ``` -------------------------------- ### Smalltalk Incubator: Original Allocation Code Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/Incubator This C code snippet demonstrates the original, potentially unsafe object allocation process before the incubator mechanism was applied. It highlights how intermediate objects could be lost if the garbage collector runs between allocations. ```c desc = (_gst_cfunc_descriptor) new_instance_with (cFuncDescriptorClass, numArgs); desc->cFunction = _gst_cobject_new (funcAddr); // 1 desc->cFunctionName = _gst_string_new (funcName); // 2 desc->numFixedArgs = FROM_INT (numArgs); desc->returnType = _gst_classify_type_symbol (returnTypeOOP, true); for (i = 1; i <= numArgs; i++) { desc->argTypes[i - 1] = _gst_classify_type_symbol(ARRAY_AT(argsOOP, i), false); } return (_gst_alloc_oop(desc)); ``` -------------------------------- ### Smalltalk Unary Message Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Demonstrates the use of unary messages in Smalltalk, where a message selector is sent to a primary object. Multiple unary messages can be chained. ```smalltalk 1234 printNl printNl printNl ``` -------------------------------- ### Smalltalk: Example usage of checksOver:do: Source: https://www.gnu.org/software/smalltalk/manual/html_node/Invoking-code-blocks This snippet demonstrates the usage of the `checksOver:do:` method. It initializes a checking account, deposits funds, writes several checks, and then uses `checksOver:do:` with different amount thresholds and a block to print check numbers that exceed the threshold. ```smalltalk mycheck := Checking new. mycheck deposit: 250 mycheck newChecks: 100 count: 40 mycheck writeCheck: 10 mycheck writeCheck: 52 mycheck writeCheck: 15 mycheck checksOver: 1 do: [:x | x printNl] mycheck checksOver: 17 do: [:x | x printNl] mycheck checksOver: 200 do: [:x | x printNl] ``` -------------------------------- ### Smalltalk Incubator: Fixed Allocation Code Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/Incubator This C code demonstrates how to correctly use the Smalltalk incubator to protect newly allocated objects. It uses INC_SAVE_POINTER, _INC_ADD_OOP, and INC_RESTORE_POINTER to ensure that objects like `desc` and `desc->cFunction` are not garbage collected prematurely. ```c OOP descOOP; IncPtr ptr; incPtr = INC_SAVE_POINTER(); desc = (_gst_cfunc_descriptor) new_instance_with (cFuncDescriptorClass, numArgs); descOOP = _gst_alloc_oop(desc); INC_ADD_OOP (descOOP); desc->cFunction = _gst_cobject_new (funcAddr); // 1 INC_ADD_OOP (desc->cFunction); desc->cFunctionName = _gst_string_new (funcName); // 2 /* since none of the rest of the function (or the functions it calls) * allocates any storage, we don’t have to add desc->cFunctionName * to the incubator’s set of objects, although we could if we wanted * to be completely safe against changes to the implementations of * the functions called from this function. */ desc->numFixedArgs = FROM_INT (numArgs); desc->returnType = _gst_classify_type_symbol (returnTypeOOP, true); for (i = 1; i <= numArgs; i++) { desc->argTypes[i - 1] = _gst_classify_type_symbol(ARRAY_AT(argsOOP, i), false); } return (_gst_alloc_oop(desc)); ``` -------------------------------- ### Call 'new' Method on Account Class in GNU Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Defining-methods This example demonstrates how to invoke the 'new' method defined for the 'Account' class. This message triggers the execution of the 'new' method, leading to the creation and initialization of a new Account object. ```smalltalk Account new ``` -------------------------------- ### RangedArray Initialization Example in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Inside-Arrays Demonstrates the creation and basic usage of a RangedArray in Smalltalk. It shows how to initialize an array with a specific size and a custom base index, and then how to place a value at a valid index within the defined range. ```Smalltalk a := RangedArray new: 10 base: 5. a at: 5 put: 0 a at: 4 put: 1 ``` -------------------------------- ### Initialize Savings Account Instance in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-Savings-class This method defines the initialization process for a new Savings account instance. It ensures that the 'interest' instance variable is set to 0 and then calls the parent class's initializer to complete the setup. This method is categorized under 'initialization'. ```smalltalk init [ interest := 0. ^super init ] ``` -------------------------------- ### Parse configuration file line with regex in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Regular-expressions This example demonstrates parsing a configuration file line using regular expressions in GNU Smalltalk. It utilizes the #=~ operator to match key-value pairs and stores them in a LookupTable. It relies on the built-in regex engine and String methods. ```smalltalk | file config | config := LookupTable new. file := (File name: 'myapp.conf') readStream. file linesDo: [:line | (line =~ '(\w+)\s*=\s*((?: ?\w+)+)') ifMatched: [:match | config at: (match at: 1) put: (match at: 2)]]. file close. config printNl. ``` -------------------------------- ### Iterate over regex matches in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Regular-expressions This example demonstrates iterating over all occurrences of a regular expression pattern within a string using the #onOccurrencesOfRegex:do: method in GNU Smalltalk. It executes a block for each successful match, starting the search from the end of the previous match. ```smalltalk stream contents onOccurrencesOfRegex: '\w+' do: [:each | each match printNl] ``` -------------------------------- ### Initialize GNU Smalltalk Environment (C) Source: https://www.gnu.org/software/smalltalk/manual/html_node/Using-Smalltalk Demonstrates the basic C code required to initialize the GNU Smalltalk environment within an application. It involves setting verbosity, passing arguments, setting the executable path, initializing the Smalltalk runtime, and optionally processing a file. ```c int main(argc, argv) int argc; char **argv; { gst_set_var (GST_VERBOSITY, 1); gst_smalltalk_args (argc - 1, argv + 1); gst_set_executable_path (argv[0]); result = gst_initialize ("kernel-dir", "image-file", GST_NO_TTY); if (result != 0) exit (result < 0 ? 1 : result); if (!gst_process_file ("source-file", GST_DIR_KERNEL_SYSTEM)) perror ("gst: couldn't load `source-file'"); gst_invoke_hook (GST_ABOUT_TO_QUIT); exit (0); } ``` -------------------------------- ### Launching the VisualGST Browser Source: https://www.gnu.org/software/smalltalk/manual/html_node/GUI Provides the command to launch the VisualGST browser. Upon execution, it loads requested packages and displays a launcher window with basic tools. ```Shell gst-browser ``` -------------------------------- ### C Struct Declaration Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/C-data-types An example of a C struct declaration for audio_prinfo. This demonstrates how to define a structure with various unsigned integer fields and an array for additional data. ```c struct audio_prinfo { unsigned channels; unsigned precision; unsigned encoding; unsigned gain; unsigned port; unsigned _xxx[4]; ``` -------------------------------- ### Alternative Example of Invoking a C Function Source: https://www.gnu.org/software/smalltalk/manual/html_node/C-callout Illustrates that the class receiving the C function method definition does not have to be `SystemDictionary`. This example shows calling the 'system' C function on the `Float` class. ```Smalltalk 1701.0 system: 'mail help-smalltalk@gnu.org' ! ``` -------------------------------- ### Restarting Smalltalk from a Snapshot Image Source: https://www.gnu.org/software/smalltalk/manual/html_node/Creating-classes Restarts the GNU Smalltalk environment from a previously saved snapshot image file. This command is executed from the shell and loads all the variables, classes, and definitions that were present when the snapshot was taken, allowing for seamless continuation of work. ```Shell $ gst -I myimage.im ``` -------------------------------- ### Define Instance Creation Method in GNU Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Defining-methods This snippet defines the 'new' method for the 'Account' class in GNU Smalltalk. It initializes a new instance, sets up its initial state by calling 'init', and returns the newly created object. The method is categorized under 'instance creation'. ```smalltalk Account class extend [ new [ | r | r := super new. r init. ^r ] ] ``` -------------------------------- ### Connect to MySQL Database Source: https://www.gnu.org/software/smalltalk/manual/html_node/Database Establishes a connection to a MySQL database using the DBI package. Requires database name, hostname, username, and password. Returns a connection object. ```Smalltalk | connection statement result | connection := DBI.Connection connect: 'dbi:MySQL:dbname=test;hostname=localhost' user: 'doe' password: 'mypass'). ``` -------------------------------- ### Build a GNU Smalltalk Module with Automake Source: https://www.gnu.org/software/smalltalk/manual/html_node/External-modules This Makefile.am snippet illustrates how to build a GNU Smalltalk module using Automake and Libtool. It specifies the library name, linker flags for creating a module, and the source files. ```makefile gstmodule_LTLIBRARIES = libdigest.la libdigest_la_LDFLAGS = -module -no-undefined _... more flags ..._ libdigest_la_SOURCES = _... your source files ..._ ``` -------------------------------- ### Displaying Help Information Source: https://www.gnu.org/software/smalltalk/manual/html_node/Invocation The `-h` or `--help` option prints a summary of the command-line syntax and definitions of all available flags, then exits the program. ```shell gst -h ``` -------------------------------- ### RBFormatter for Pretty-Printing Source: https://www.gnu.org/software/smalltalk/manual/html_node/Smalltalk_002din_002dSmalltalk Demonstrates the use of the visitor pattern implementation via the RBFormatter class for pretty-printing Smalltalk code. This showcases how visitors can process parse trees. ```Smalltalk Object subclass: #RBFormatter instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Parser-Formatting' ``` -------------------------------- ### Smalltalk Syntax for Pointers and Arrays in C Structs Source: https://www.gnu.org/software/smalltalk/manual/html_node/C-data-types This snippet details the Smalltalk syntax for declaring pointers and arrays within C structures. '(#example (#ptr #long))' represents a pointer to a long, while '(#example (#array #string size))' defines an array of strings with a specified size. These constructs facilitate the representation of complex C data types. ```Smalltalk (#example (#ptr #long)) (#example (#array #string size)) ``` -------------------------------- ### C: Handle SQL Object Creation and Updates Source: https://www.gnu.org/software/smalltalk/manual/html_node/Object-representation This C code snippet demonstrates how to handle different types of SQL requests within the GNU Smalltalk C API. It differentiates between object creation and query update scenarios, assigning results to appropriate C structures and converting C data to Smalltalk objects using `vmProxy` functions. It also includes memory deallocation for requests. ```c /* Free the memory allocated by oopToString */ free(request); if (OOP_CLASS (oop) == sqlActionClass) return; if (OOP_CLASS (oop) == sqlObjectCreationClass) { SQLObjectCreation oc; oc = (SQLObjectCreation) OOP_TO_OBJ (clientData); cObject = vmProxy->cObjectToOOP (result->dbObject) oc->newDBObject = cObject; } else { /* SQLRequest or SQLUpdateQuery */ cObject = vmProxy->cObjectToOOP (result->rows); query = (SQLUpdateQuery) OOP_TO_OBJ (clientData); query->returnedRows = cObject; if (OOP_CLASS (oop) == sqlUpdateQueryClass) query->numReturnedRows = vmProxy->intToOOP (result->count); } ``` -------------------------------- ### Load Seaside Packages (Shell) Source: https://www.gnu.org/software/smalltalk/manual/html_node/Seaside Command to load Seaside and related development/example packages into a GNU Smalltalk image. This is a prerequisite for running Seaside applications. ```shell $ gst-load -iI seaside.im Seaside Seaside-Development Seaside-Examples ``` -------------------------------- ### Exploring Class Hierarchy in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Why-is-_0023new-there_003f_0021_003f Demonstrates how to traverse the superclass hierarchy for various objects and classes in Smalltalk, starting from Set and ending at Object. ```smalltalk st> `Set superclass!` HashedCollection st> `HashedCollection superclass!` Collection st> `Collection superclass!` Object st> `Object superclass!` nil ``` -------------------------------- ### Smalltalk: Print 'Hello, world' Source: https://www.gnu.org/software/smalltalk/manual/html_node/Saying-hello This code snippet demonstrates how to print the string 'Hello, world' to the console in GNU Smalltalk. The `printNl` method is used for this purpose. The output appears twice due to the direct evaluation of the string. ```smalltalk 'Hello, world' printNl ``` -------------------------------- ### Smalltalk Byte Array Constant Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Illustrates the syntax for creating a ByteArray constant in Smalltalk. Elements in a ByteArray must be integers between 0 and 255. ```smalltalk a := #[1 2 34 16r8F 26r3H 253] ``` -------------------------------- ### Smalltalk Stream on New String Source: https://www.gnu.org/software/smalltalk/manual/html_node/Your-own-stream Illustrates creating a ReadWriteStream on a new, empty string and appending data using `nextPutAll:`, then inspecting the contents. ```smalltalk s := ReadWriteStream on: String new s inspect s nextPutAll: 'Hello, ' s inspect s nextPutAll: 'world' s inspect s position: 1 s inspect s do: [:c | stdout nextPut: c ] s contents ``` -------------------------------- ### Smalltalk Absolute Pathname Reference Source: https://www.gnu.org/software/smalltalk/manual/html_node/Namespaces Shows how to reference a global object using its absolute 'full pathname' starting from the root environment, typically 'Smalltalk'. Symbols are separated by periods. ```Smalltalk Smalltalk.Tasks.MyTask ``` -------------------------------- ### Using '--' to Stop Option Interpretation Source: https://www.gnu.org/software/smalltalk/manual/html_node/Invocation The '--' argument signifies the end of options. All subsequent arguments are treated as file names, even if they start with a hyphen. ```shell gst -- --my-file-starting-with-dash.st ``` -------------------------------- ### GNU Smalltalk: Get OOP for Object's Class Source: https://www.gnu.org/software/smalltalk/manual/html_node/Other-C-functions Retrieves the OOP representing the class of a given object. This is essential for introspection and determining an object's type. ```C Macro: **OOP** _OOP_CLASS (OOP)_ ``` -------------------------------- ### Print a GNU Smalltalk Dictionary Source: https://www.gnu.org/software/smalltalk/manual/html_node/Dictionaries Illustrates how to print the entire contents of a dictionary. The output clearly shows the key-value pairs, with the key preceding the value in each pair. ```Smalltalk y ``` -------------------------------- ### Retry Block Execution on SomeError Exception in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/Handling-exceptions This example shows how to use #on:do: with the 'retry' message to restart a block of code if a 'SomeError' exception is encountered. This is useful for implementing recovery mechanisms without increasing stack depth. ```Smalltalk frobnicate: n [ ^[do some stuff with n] on: SomeError do: [:sig | sig return: (self frobnicate: n + 1)] ] ``` -------------------------------- ### Smalltalk Expression Sequence Syntax Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-syntax Illustrates how sequences of expressions are structured in Smalltalk, separated by dots. It also covers the syntax for returning a value from an expression sequence using the caret symbol. ```smalltalk exprs: [expr ``.'']* [[``^''] expr] ``` -------------------------------- ### Create and Populate a GNU Smalltalk Dictionary Source: https://www.gnu.org/software/smalltalk/manual/html_node/Dictionaries Demonstrates the creation of a new dictionary and populating it with key-value pairs. Keys and values can be of any object type, including strings and integers. Dictionaries are less efficient than arrays but offer powerful data correlation. ```Smalltalk y := Dictionary new y at: 'One' put: 1 y at: 'Two' put: 2 y at: 1 put: 'One' y at: 2 put: 'Two' ``` -------------------------------- ### Define Savings Class as Subclass of Account in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-Savings-class This snippet defines the Savings class as a subclass of the Account class in Smalltalk. It declares an instance variable 'interest' to track accumulated interest. The class definition is started here and will be closed later. ```smalltalk Account subclass: Savings [ | interest | ``` -------------------------------- ### Checking Expected Results with #should: (Smalltalk) Source: https://www.gnu.org/software/smalltalk/manual/html_node/SUnit Illustrates the use of the '#should:' method in SUnit to assert that a given block of code evaluates to true. If the block returns false, the test case fails and records the error. This example checks if an element is included in a set. ```smalltalk SetTestCase>>#testAdd empty add: 5. self should: [empty includes: 5] ``` -------------------------------- ### Print to stdout and inspect Source: https://www.gnu.org/software/smalltalk/manual/html_node/The-output-stream This snippet demonstrates printing a string to the standard output stream (stdout) and then inspecting its contents. It highlights direct interaction with the stdout global. ```Smalltalk 'Hello, world' printOn: stdout stdout inspect ``` -------------------------------- ### Smalltalk-80 Error Signaling Example Source: https://www.gnu.org/software/smalltalk/manual/html_node/Exception-handling Demonstrates the traditional Smalltalk-80 error signaling mechanism using the `#error:` message. When an error condition is met, the stack is unwound, and an error message is printed along with stack information. This method is suitable for unrecoverable errors. ```Smalltalk check: num [ | c | c := history at: num ifAbsent: [ ^self error: 'No such check #' ]. ^c ] ``` -------------------------------- ### Instantiate Account and Default Print Source: https://www.gnu.org/software/smalltalk/manual/html_node/A-look-at-our-object This snippet demonstrates how to create a new instance of the Account class and shows the default, uninformative output when the object is printed. It highlights the need for a custom printing method. ```smalltalk a := Account new an Account ``` -------------------------------- ### Smalltalk Class Hierarchy Example 3: Parallel Inheritance Source: https://www.gnu.org/software/smalltalk/manual/html_node/Animals This code snippet illustrates the final Smalltalk class hierarchy where both Parrots and Pigs inherit directly from Animals, but not from each other. This structure is presented as a more effective way to organize the classes. ```text Object Animals Parrots Pigs ``` -------------------------------- ### Smalltalk Stream with next:put: Source: https://www.gnu.org/software/smalltalk/manual/html_node/Your-own-stream Shows how to use `next:put:` to write an object multiple times to a stream initialized with an empty array. ```smalltalk s := ReadWriteStream on: (Array new: 0) s next: 4 put: 'Hi!' s position: 1 s do: [:x | x printNl] ``` -------------------------------- ### Smalltalk Class Hierarchy Example 2: Nested Inheritance Source: https://www.gnu.org/software/smalltalk/manual/html_node/Animals This code snippet presents a nested inheritance hierarchy in Smalltalk where Parrots inherit from Animals, and Pigs inherit from Parrots. This structure aims to reduce redundancy by leveraging inheritance. ```text Object Animals Parrots Pigs ``` -------------------------------- ### Open Library and Initialize Module in Smalltalk Source: https://www.gnu.org/software/smalltalk/manual/html_node/External-modules This Smalltalk function, `dlOpen`, attempts to open a dynamic library specified by filename. If the library contains `gst_initModule`, it will be invoked. The `module` parameter controls whether the file is added to the global search path. ```smalltalk _mst_Boolean_ **dlOpen** _(void *filename, int module)_ ```