Beginning Ring Programming From Novice to Professional Mansour Ayouni
Beginning Ring Programming: From Novice to Professional
ISBN-13 (pbk): 978-1-4842-5832-3 ISBN-13 (electronic): 978-1-4842-5833-0https://doi.org/10.1007/978-1-4842-5833-0
Copyright © 2020 by Mansour Ayouni
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed SpahrAcquisitions Editor: Steve AnglinDevelopment Editor: Matthew MoodieCoordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image by Lucas Lenzi on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please email [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484258323. For more detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper
Mansour AyouniKalidia ConsultingSousse, Tunisia
v
Table of Contents
Chapter 1: Getting Started with Ring ������������������������������������������������������������������������ 1
What You Will Learn ���������������������������������������������������������������������������������������������������������������������� 2
Download and Installation ������������������������������������������������������������������������������������������������������������� 2
Selecting the Appropriate Installer ������������������������������������������������������������������������������������������ 3
Discovering the Ring Installation Folder ���������������������������������������������������������������������������������� 4
Launching Ring Notepad ��������������������������������������������������������������������������������������������������������� 6
Your First Ring Program ���������������������������������������������������������������������������������������������������������������� 7
Organizing Yourself ������������������������������������������������������������������������������������������������������������������ 7
Asking Ring Questions ������������������������������������������������������������������������������������������������������������� 8
From Date to Time ����������������������������������������������������������������������������������������������������������������� 10
A Ring Flower: timeList Function ������������������������������������������������������������������������������������������� 11
Making timeList Expressive ��������������������������������������������������������������������������������������������������� 12
Listening to Ring When It Complains ������������������������������������������������������������������������������������� 13
Playing with Lists ������������������������������������������������������������������������������������������������������������������ 14
Expressiveness: First Round �������������������������������������������������������������������������������������������������� 16
Expressiveness: Second Round ��������������������������������������������������������������������������������������������� 17
The Final Listing of the “Hello Ring!” Program ���������������������������������������������������������������������� 19
The “Very Final” Listing: One More Adjustment!�������������������������������������������������������������������� 20
Documentation and the Help System ������������������������������������������������������������������������������������������ 23
About the Author ���������������������������������������������������������������������������������������������������� xv
About the Technical Reviewer ������������������������������������������������������������������������������ xvii
Acknowledgments ������������������������������������������������������������������������������������������������� xix
Introduction ����������������������������������������������������������������������������������������������������������� xxi
Foreword �������������������������������������������������������������������������������������������������������������� xxv
vi
Any “Hello, World!” Tutorial? �������������������������������������������������������������������������������������������������� 25
Introductory Article by Mahmoud Fayed �������������������������������������������������������������������������������� 34
Using the Ring Help System �������������������������������������������������������������������������������������������������� 37
Other Resources on Ring ������������������������������������������������������������������������������������������������������� 48
Summary������������������������������������������������������������������������������������������������������������������������������������� 49
Chapter 2: Data Types in Ring ��������������������������������������������������������������������������������� 51
Nine Things You Will Learn ���������������������������������������������������������������������������������������������������������� 51
How Ring Sees the World ������������������������������������������������������������������������������������������������������������ 52
Types and Type System: A First Flavor ����������������������������������������������������������������������������������� 53
Meet the Gang of Four ����������������������������������������������������������������������������������������������������������� 53
Missing Members of the Gang ����������������������������������������������������������������������������������������������� 56
A Little Bit of Magic ��������������������������������������������������������������������������������������������������������������� 62
Statics and Dynamics of the Ring Type System �������������������������������������������������������������������������� 66
A Reference Architecture ������������������������������������������������������������������������������������������������������� 66
Checking Data Types ������������������������������������������������������������������������������������������������������������� 70
Converting Between Data Types �������������������������������������������������������������������������������������������� 71
User-Defined Data Types ������������������������������������������������������������������������������������������������������� 74
Implicit Conversion and Lexical Scoping ������������������������������������������������������������������������������� 83
Under the Hood: C Language Type System ���������������������������������������������������������������������������� 84
More on Data Types ��������������������������������������������������������������������������������������������������������������������� 85
More on Numbers ������������������������������������������������������������������������������������������������������������������ 86
More on Strings ��������������������������������������������������������������������������������������������������������������������� 87
More on Objects �������������������������������������������������������������������������������������������������������������������� 90
More on Lists ������������������������������������������������������������������������������������������������������������������������� 93
Capturing the Potential of Dynamic Ring ������������������������������������������������������������������������������������ 96
Summary������������������������������������������������������������������������������������������������������������������������������������� 98
Chapter 3: Inputs to Ring ���������������������������������������������������������������������������������������� 99
Nine Things You Will Learn �������������������������������������������������������������������������������������������������������� 100
Inputs in the Code ��������������������������������������������������������������������������������������������������������������������� 101
Using Variables to Host Data in Code ���������������������������������������������������������������������������������� 101
Table of ConTenTs
vii
Avoiding Magical Numbers �������������������������������������������������������������������������������������������������� 103
Mitigating Code Complexity ������������������������������������������������������������������������������������������������� 106
Refactoring the Data Side ���������������������������������������������������������������������������������������������������� 108
Refactoring the Code Side ��������������������������������������������������������������������������������������������������� 110
Separating Between Concerns �������������������������������������������������������������������������������������������� 113
Using Global Variables ��������������������������������������������������������������������������������������������������������� 116
Inputs in the Console ���������������������������������������������������������������������������������������������������������������� 119
Using the Give Command ���������������������������������������������������������������������������������������������������� 119
Getting Arguments from the Command Line ����������������������������������������������������������������������� 121
Inputs from the GUI ������������������������������������������������������������������������������������������������������������������� 122
Creating Windows in the Form Designer ����������������������������������������������������������������������������� 122
Understanding the Generated Files from the Form Designer ���������������������������������������������� 129
Understanding Events���������������������������������������������������������������������������������������������������������� 132
Fabricating the User Interface ��������������������������������������������������������������������������������������������� 135
Responding to User Events �������������������������������������������������������������������������������������������������� 138
Enhancing the User Experience ������������������������������������������������������������������������������������������� 143
Three Values of MVC in GUI Design ������������������������������������������������������������������������������������� 146
Inputs from Text Files ���������������������������������������������������������������������������������������������������������������� 150
Reading Data from a Text File ���������������������������������������������������������������������������������������������� 150
Specifying the NorthAfrica App ������������������������������������������������������������������������������������������� 151
Seven Steps to the NorthAfrica App ������������������������������������������������������������������������������������ 152
Creating Conventions and Configuration Files ��������������������������������������������������������������������� 153
Transforming Text Data into a Ring List ������������������������������������������������������������������������������� 154
Preparing Data for Showing a Graph ����������������������������������������������������������������������������������� 157
Rendering the Graph Inside a Text String ���������������������������������������������������������������������������� 165
Inputs from the Database���������������������������������������������������������������������������������������������������������� 168
Connecting to a SQLite Database ���������������������������������������������������������������������������������������� 168
Designing a Clean Data Acquisition Architecture ���������������������������������������������������������������� 170
Switching Between Text and Database Data Sources ��������������������������������������������������������� 172
Implementing a Universal Data Connector �������������������������������������������������������������������������� 174
Adapting the Connector to the Database ����������������������������������������������������������������������������� 178
Table of ConTenTs
viii
Unifying the Data Format Delivered by the Connector �������������������������������������������������������� 180
Testing Our Data Acquisition Architecture ��������������������������������������������������������������������������� 184
Final Refinement ����������������������������������������������������������������������������������������������������������������������� 184
Other Inputs ������������������������������������������������������������������������������������������������������������������������������ 188
Summary����������������������������������������������������������������������������������������������������������������������������������� 189
Chapter 4: Outputs of Ring ����������������������������������������������������������������������������������� 191
Nine Things You Will Learn �������������������������������������������������������������������������������������������������������� 191
Before We Start ������������������������������������������������������������������������������������������������������������������������� 192
Refactoring the Right Side �������������������������������������������������������������������������������������������������������� 193
Analyzing the Existing Architecture ������������������������������������������������������������������������������������� 194
Designing a Target Architecture ������������������������������������������������������������������������������������������ 198
Using See Instead of ? ��������������������������������������������������������������������������������������������������������� 201
Inviting BASIC into Ring ������������������������������������������������������������������������������������������������������� 202
Using the Standard print( ) Function ������������������������������������������������������������������������������������ 204
Output to a File �������������������������������������������������������������������������������������������������������������������������� 206
Output to a Database ���������������������������������������������������������������������������������������������������������������� 208
Output to a Web Page ���������������������������������������������������������������������������������������������������������������� 211
Studying the Feasibility of Printing cGraph in a Web Page �������������������������������������������������� 213
Yet Another Architecture Refactoring ���������������������������������������������������������������������������������� 215
Correctly Implementing the Web Target ������������������������������������������������������������������������������ 218
Writing HTML in Ring ����������������������������������������������������������������������������������������������������������� 221
Output to a Printed File ������������������������������������������������������������������������������������������������������������� 223
What’s Next ������������������������������������������������������������������������������������������������������������������������������� 228
Chapter 5: Crafting Ring Algorithms ��������������������������������������������������������������������� 229
Nine Things You Will Learn �������������������������������������������������������������������������������������������������������� 230
Before We Start: Long Live Visuality! ���������������������������������������������������������������������������������������� 230
A Snapshot of Drakon: What and Why? ������������������������������������������������������������������������������������� 231
Three Forms of Logic Flows ������������������������������������������������������������������������������������������������������ 235
Sequence ����������������������������������������������������������������������������������������������������������������������������� 235
Selection (or Branching) ������������������������������������������������������������������������������������������������������ 239
Table of ConTenTs
ix
Iteration (or Looping) ����������������������������������������������������������������������������������������������������������� 242
Difference Between for and for in ��������������������������������������������������������������������������������������� 245
Infinite Loops ����������������������������������������������������������������������������������������������������������������������� 248
More on Logic Flows ����������������������������������������������������������������������������������������������������������������� 250
Vertical Selections ��������������������������������������������������������������������������������������������������������������� 250
Deep Selections ������������������������������������������������������������������������������������������������������������������� 254
Condition-First Iteration: while �������������������������������������������������������������������������������������������� 265
Condition-Last Iteration: do again ��������������������������������������������������������������������������������������� 274
Multiple Selections �������������������������������������������������������������������������������������������������������������� 276
Other Ring Control Structure Techniques ���������������������������������������������������������������������������������� 277
Using for in to Modify Lists �������������������������������������������������������������������������������������������������� 278
Stepping with the for Statement ����������������������������������������������������������������������������������������� 279
Exiting from One or Two Loops �������������������������������������������������������������������������������������������� 280
Using the loop Keyword ������������������������������������������������������������������������������������������������������� 281
Short-Circuit Evaluation ������������������������������������������������������������������������������������������������������� 281
Exceptions (try/catch) ���������������������������������������������������������������������������������������������������������� 283
Useful Details About Evaluation ������������������������������������������������������������������������������������������� 284
Three Commandments of the Logic Craftperson ���������������������������������������������������������������������� 284
The First Commandment: Clean Order �������������������������������������������������������������������������������� 285
The Second Commandment: One Start, One End ���������������������������������������������������������������� 287
The Third Commandment: Clean Logical Parts �������������������������������������������������������������������� 289
Before You Leave: Homework! �������������������������������������������������������������������������������������������������� 291
Summary����������������������������������������������������������������������������������������������������������������������������������� 292
Chapter 6: Juggling with Scopes �������������������������������������������������������������������������� 293
Nine Things You Will Learn �������������������������������������������������������������������������������������������������������� 293
Scopes: Why Should You Care? ������������������������������������������������������������������������������������������������� 294
Three Variable Scopes: Global, Local, and Object ��������������������������������������������������������������������� 295
Global Scope ������������������������������������������������������������������������������������������������������������������������ 295
Local Scope ������������������������������������������������������������������������������������������������������������������������� 299
Object Scope ����������������������������������������������������������������������������������������������������������������������� 303
Managing Conflictual Scopes���������������������������������������������������������������������������������������������������� 319
Table of ConTenTs
x
Conflict Between the Global and Object Scopes ����������������������������������������������������������������� 319
Conflict Between the Object and Local Scopes ������������������������������������������������������������������� 323
Conflict Between the Global and Local Scopes ������������������������������������������������������������������� 326
Tracing Variables Using TraceLib ����������������������������������������������������������������������������������������������� 326
A Gift: Extended wapp�ring Program ����������������������������������������������������������������������������������������� 332
Summary����������������������������������������������������������������������������������������������������������������������������������� 335
Chapter 7: Functions, Objects, and Beyond ���������������������������������������������������������� 337
Nine Things You Will Learn �������������������������������������������������������������������������������������������������������� 338
Functions and Functional Style ������������������������������������������������������������������������������������������������� 339
Defining and Calling Functions �������������������������������������������������������������������������������������������� 339
Sending Parameters and Returning Values ������������������������������������������������������������������������� 342
Using a List for Optional Parameters ����������������������������������������������������������������������������������� 347
Recursion: When a Function Calls Itself ������������������������������������������������������������������������������ 350
Beyond Functions, the Functional Programming Paradigm ������������������������������������������������ 352
Objects and Object Orientation ������������������������������������������������������������������������������������������������� 363
Creating Objects from Classes �������������������������������������������������������������������������������������������� 364
Initializing Objects ��������������������������������������������������������������������������������������������������������������� 366
Accessing Objects ��������������������������������������������������������������������������������������������������������������� 367
Using Setters and Getters ���������������������������������������������������������������������������������������������������� 369
Sending and Returning Objects ������������������������������������������������������������������������������������������� 373
A Recap: Object Structure and Class Event Mechanism ������������������������������������������������������ 376
Inheritance and Composition ����������������������������������������������������������������������������������������������� 377
Securing the Object World with Encapsulation �������������������������������������������������������������������� 393
Managing a Collection of Objects ���������������������������������������������������������������������������������������� 396
Changing the Object Operators ������������������������������������������������������������������������������������������� 400
Interacting with Objects Inside Braces �������������������������������������������������������������������������������� 402
Beyond Objects, Declarative Programming in Ring ������������������������������������������������������������� 404
Beyond Objects, Natural Programming in Ring ������������������������������������������������������������������� 417
Summary����������������������������������������������������������������������������������������������������������������������������������� 429
Table of ConTenTs
xi
Chapter 8: Designing Games in Ring �������������������������������������������������������������������� 431
Nine Things You Will Learn �������������������������������������������������������������������������������������������������������� 432
A Personal Perspective on Game Programming ����������������������������������������������������������������������� 433
Three Fundamentals of Game Programming ���������������������������������������������������������������������������� 438
Game Loop ��������������������������������������������������������������������������������������������������������������������������� 438
Game Timing ������������������������������������������������������������������������������������������������������������������������ 441
Game Engine ����������������������������������������������������������������������������������������������������������������������� 444
Mastering the Big Picture ��������������������������������������������������������������������������������������������������������� 451
Twelve Practical Skills in Your Bucket �������������������������������������������������������������������������������������� 456
Skill 1: Opening a Window to Host the Game ���������������������������������������������������������������������� 456
Skill 2: Setting the Ambiance ����������������������������������������������������������������������������������������������� 458
Skill 3: Populating the Game with Objects ��������������������������������������������������������������������������� 462
Skill 4: Animating Objects ���������������������������������������������������������������������������������������������������� 469
Skill 5: Intervening in the Game State ��������������������������������������������������������������������������������� 472
Skill 6: Querying Game Objects ������������������������������������������������������������������������������������������� 474
Skill 7: Intercepting Events �������������������������������������������������������������������������������������������������� 479
Skill 8: Detecting Collisions ������������������������������������������������������������������������������������������������� 488
Skill 9: Implementing and Testing the Gameplay ����������������������������������������������������������������� 491
Skill 10: Organizing and Refactoring Code �������������������������������������������������������������������������� 496
Skill 11: Distributing Your Game on Many Platforms ����������������������������������������������������������� 501
Skill 12: Monitoring Your Game Performance ���������������������������������������������������������������������� 503
A Complete Game in Ring ��������������������������������������������������������������������������������������������������������� 505
Before You Make It, Play It! �������������������������������������������������������������������������������������������������� 505
Loading Globals ������������������������������������������������������������������������������������������������������������������� 506
Write the Game Loop ����������������������������������������������������������������������������������������������������������� 507
Write the Game World Function ������������������������������������������������������������������������������������������� 507
Write the Game Initialization Function ��������������������������������������������������������������������������������� 509
Protect the World Frontiers �������������������������������������������������������������������������������������������������� 510
Attack the Enemies �������������������������������������������������������������������������������������������������������������� 511
Accept to Be Attacked! �������������������������������������������������������������������������������������������������������� 512
You’re Captured, Write a Collision Function ������������������������������������������������������������������������� 513
Table of ConTenTs
xii
Destroy the Enemies, Without Mercy! ���������������������������������������������������������������������������������� 514
It Exploded, Show the Fire! �������������������������������������������������������������������������������������������������� 515
Monitor Your Energy ������������������������������������������������������������������������������������������������������������ 515
Grow the Stars, They Are Bonuses! ������������������������������������������������������������������������������������� 516
The Stars, Captured! ������������������������������������������������������������������������������������������������������������ 517
To Be Captured, They Must Be Harvested ���������������������������������������������������������������������������� 517
Your Score, Updated ������������������������������������������������������������������������������������������������������������ 518
Play Again or Quit? �������������������������������������������������������������������������������������������������������������� 518
Sure, I Want to Play Again! ��������������������������������������������������������������������������������������������������� 519
Game Over! �������������������������������������������������������������������������������������������������������������������������� 519
Beyond Gaming, Video Games Culture �������������������������������������������������������������������������������������� 521
Chapter 9: A Gamified Foundation for Mastering Ring ����������������������������������������� 523
Nine Things You Will Learn �������������������������������������������������������������������������������������������������������� 526
Programming: Toward a New Metaphor ������������������������������������������������������������������������������������ 526
A Critic of the “Construction” Metaphor ������������������������������������������������������������������������������ 527
The Problem of Software Construction �������������������������������������������������������������������������������� 530
Design of a Solution Strategy ���������������������������������������������������������������������������������������������� 536
Gaming as a New Metaphor for Programming �������������������������������������������������������������������� 537
A Target Skillset of Our Gamified Learning Experience ������������������������������������������������������� 552
Ringoria, the Programming Land of Ring ���������������������������������������������������������������������������������� 557
The Ringorialand Map���������������������������������������������������������������������������������������������������������� 558
The Floating Castle �������������������������������������������������������������������������������������������������������������� 559
Watermania ������������������������������������������������������������������������������������������������������������������������� 560
The Flower of Athingza �������������������������������������������������������������������������������������������������������� 562
The Bay of Sand ������������������������������������������������������������������������������������������������������������������ 563
The Ringuter ������������������������������������������������������������������������������������������������������������������������ 564
The Cap of Lights ����������������������������������������������������������������������������������������������������������������� 565
The Training Camp ��������������������������������������������������������������������������������������������������������������� 567
The Underground Station ����������������������������������������������������������������������������������������������������� 569
The Royal Library ����������������������������������������������������������������������������������������������������������������� 570
Table of ConTenTs
xiii
In the Pocket of Every Ringler ��������������������������������������������������������������������������������������������������� 571
An Iron Compass ����������������������������������������������������������������������������������������������������������������� 572
A Ringometer ����������������������������������������������������������������������������������������������������������������������� 575
A Sociogram ������������������������������������������������������������������������������������������������������������������������ 577
A Set of Teamcards �������������������������������������������������������������������������������������������������������������� 578
A Mission “Titiey” ���������������������������������������������������������������������������������������������������������������� 581
A Spectrogram ��������������������������������������������������������������������������������������������������������������������� 582
Rules of the Game ��������������������������������������������������������������������������������������������������������������������� 583
Believe in Ringaros �������������������������������������������������������������������������������������������������������������� 584
Follow the Roadmap of King Ringamesh ����������������������������������������������������������������������������� 594
Ringza by Heart, Ringza by Hand! ��������������������������������������������������������������������������������������� 596
Harvest the Stars, Design a Learning Path �������������������������������������������������������������������������� 610
You Are the Ringler! ������������������������������������������������������������������������������������������������������������� 613
Conclusion �������������������������������������������������������������������������������������������������������������������������������� 616
Appendix A: A Dialogue with Mahmoud Fayed ����������������������������������������������������� 619
Network Programming in Ring �������������������������������������������������������������������������������������������������� 619
Composition over Inheritance ��������������������������������������������������������������������������������������������������� 620
Parallelization in Ring ��������������������������������������������������������������������������������������������������������������� 621
A Language Copying Ring ��������������������������������������������������������������������������������������������������������� 621
Ring Data Type System ������������������������������������������������������������������������������������������������������������� 622
About the Ring Compiler and VM ���������������������������������������������������������������������������������������������� 624
When the Default Value of NULL Is Changed ����������������������������������������������������������������������������� 628
Speed of Ring List vs� C Code Array ������������������������������������������������������������������������������������������ 629
Level Designer in Ring Games �������������������������������������������������������������������������������������������������� 634
Advice on the Use of Operator Overloading ������������������������������������������������������������������������������ 636
Numeric Overflow Error ������������������������������������������������������������������������������������������������������������ 638
A Strange Behavior in len( ) of Lists ������������������������������������������������������������������������������������������ 642
Your Approach to Testing ����������������������������������������������������������������������������������������������������������� 643
The Secret Behind Mahmoud’s Responsiveness ���������������������������������������������������������������������� 645
The Story Behind the Name of Ring ������������������������������������������������������������������������������������������ 648
Table of ConTenTs
xiv
Ring vs� Python ������������������������������������������������������������������������������������������������������������������������� 649
Ring in 10 Years ������������������������������������������������������������������������������������������������������������������������ 649
Monetizing PWCT ���������������������������������������������������������������������������������������������������������������������� 649
Ring Foundation ������������������������������������������������������������������������������������������������������������������������ 650
Ring vs� PWCT ��������������������������������������������������������������������������������������������������������������������������� 650
Your Final Word to the Reader ��������������������������������������������������������������������������������������������������� 651
Index ��������������������������������������������������������������������������������������������������������������������� 653
Table of ConTenTs
xv
About the Author
Mansour Ayouni is one of the leading contributors to the
Ring programming language. He wrote his first BASIC
computer statement in the mid-80s, using only pen and
paper, when he was 10 years old. It wasn’t in a California
garage, though, or under the lights of the prestigious Eiffel
Tower of Paris; it was in the rural village of Regueb in
the center of Tunisia (North Africa) where there was no
electricity or computers at all. Over the years, programming
took him on an otherwise impossible international journey.
From Tunisia to Niger to Canada, he contributed to the
development of dozens of software applications ranging
from lawyer office management solutions to banking to nuclear waste management
systems. During his career, he has overseen programmers from three cultures and led
software teams in various companies such as Whitecape, Keyrus, and Webgenetics. Now,
he is a member of the Ring core team and runs Kalidia Consulting, helping businesses to
make effective software.
xvii
About the Technical Reviewer
Bert Mariani has worked on mainframe hardware as a field
engineer for Univac and Amdahl, where machine language
coding was used to debug and fix hardware problems when
the OS would not boot. He supplements his coding skills
with Assembler and C Code.
Bert currently resides in Montreal, Canada, where he
works for Bell Mobility, helping to migrate 4G technology
to 5G. Along with his team, he tests and implements many
different types of network equipment from various hardware
vendors to support mobile communications and cell
phones.
xix
Acknowledgments
To Cherihen, Teeba, and Haneen for their love, encouragement, and unbounded
sacrifice.
To Mahmoud Fayed, Bert Mariani, and the Apress team (Mark, Matt, and Steve) for
their support, patience, and dedication.
To those who inspired and taught me something during my programming career.
xxi
Introduction
In Tunisia, a nation that’s more than 3,000 years old and my motherland, ancient
building engineers left us the second biggest Roman theater in the world (Al-Jam), the
first Islamic university (Al-Zaytoona), and the prestigious Cathedral of St. Vincent de
Paul in the heart of the capital, Tunis. They are all beautiful and still functional, and they
continuously inspire architects and artists alike. They are civilizational achievements
that convey intellectual supremacy, design elegance, and timeless rays of spirituality.
In this book, I try to keep track of my grandparent’s footsteps, and make a modest
contribution to the building of Software Civilization, by providing a new learning
experience of the art of programming, for a new programming language called Ring…
RingLike a beautiful star, Ring shines in the sky of the new generation of programming
languages because of its outstanding learnability, expressiveness, and extreme
flexibility. The language is remarkably dogma-free and embraces declarative and natural
programming in an effective new way. Yet it is a serious tool, with powerful features and
extended portability. It is ranked in the top 100 most popular programming languages
(https://www.tiobe.com/tiobe-index/) and has been used to develop the PWCT
open source visual programming project, which more than 21 million downloads from
SourceForge.
The BookThe main goal of Beginning Ring Programming is to help you gain a gentle yet rigorous
introduction to the world of Ring programming with clarity as the first concern, rich
visualizations, and a lot of practical code examples.
In this book, Ring is used more as a computational thinking medium than a
programming language. Its simple pseudocode-like footprint will help you focus on the
underlining logic and ideas instead of the code itself. Its flexible syntax allows anyone,
xxii
whatever language programming language they use, to feel at home when reading and
writing computer code. Several programming paradigms (structured, object-oriented,
functional, declarative, natural language, and metaprogramming), all allowed by Ring,
are used in combination to compose specific designs to address specific problems in
different situations.
Part I shows you how to install Ring and gives you a bird’s-eye view of the language
by exploring its features. I explain how Ring rigorously structures programs, and then
you’ll learn about the type system of the language by exposing its four native data types
(number, string, list, and object) and how you can augment them by crafting your own
user-defined types.
Part II is where you will learn how Ring manages inputs, outputs, and what is in
between. The basic constructs of computer logic (sequence, selection, and iteration) are
used in isolation and then combined to show more complex logic flows. I will discuss
real-world scenarios and common mistakes leading to code complexity and present
several strategies of refactoring, code cleansing, data modeling, and variable naming.
Then, I will explain how Ring deals with the scopes of variables, at the local, object, and
global levels.
In Part III, you will master the two conceptual constructs every programmer truly
needs: functions and objects. You’ll discover how they can be composed to solve a
problem and how more advanced programming paradigms, such as declarative and
natural, are beautifully implemented on top of them. Then I will introduce game
programming as one of the domain applications of Ring. You’ll learn how you can design
a game declaratively, in Ring code, like you were designing it in visual software. Finally,
I’ll deliver 30 years of experience in writing code in the form of a gamified Ring-oriented
fantasy world called Ringorialand. You’ll learn how much of programming is complex,
both technically and socially, and how gaming can be a good metaphor to reign over it.
The book was written over a period of nine months, with a lot of patience,
consideration, and love of sharing. It is not intended to be a clinical user manual of
Ring, though, but rather a passionate alchemy of technical knowledge, real-world
programming experience, and critical personal reflections. So, use it to build your
technical programming skills in Ring while developing a cultural consciousness of
programming from the intellectual conversation it contains between you, the reader,
and me, a seasoned programmer questioning today’s code complexity via an emergent
programming language that provides elegant and unprecedented answers to those
questions.
InTroduCTIon
xxiii
Beginning Ring Programming is for those who are passionate about the
craftsmanship culture in writing code, thinking in code, and solving algorithmic
problems in a beautiful, expressive, and learnable programming language. You’ll gain
beginner-friendly knowledge about Ring and benefit, at the same time, from a one-stop
container of lessons learned and valuable returns on experience, all distilled into real-
world, customer-facing programming projects.
What You Will LearnSpecifically, in this book, you will do the following:
• Get started with the Ring programming language
• Master data types, I/O, GUI widgets, events, functions, and classes,
including how they can be composed
• Carry out structural, object-oriented, functional, declarative, natural,
and metaprogramming with Ring
• Quickly design professional-grade video games on top of the Ring
Game Engine and enjoy a new declarative gaming programming
experience with Ring
• Use the full power of Ring to refactor program code and develop
clean and flexible program architectures
• Deploy visual thinking techniques in pragmatically solving complex
algorithmic problems in Ring
• Embrace the Ring language culture founded on syntax freedom,
programmer responsibility, hackable transparency, and dogma-free
multiparadigm programming.
InTroduCTIon
xxv
Foreword
Ring was created with a goal: to be a new language for a new programming experience.
Developing a programming language that we can rely on when we are thinking
about the problems we need to solve instead of being constrained by the syntax and
the rigidity of the programming paradigm is not an easy endeavor. The general mindset
cultivated by the programming domain so far has proliferated the establishment of a set
of technical dogmas. Obviously, this narrows the window of innovation and constitutes
a serious barrier to entry for many people who could be great programmers but are
demotivated by the complexity of today’s programming tools.
Facing such a cultural and technological challenge, I was aware from the beginning
that I should develop a programming language for the long term. Although Ring, as
you will discover in this book, is designed to deal pragmatically with any situation
that a general-purpose programming language can manage, my focus was to enable
new ways of thinking in code, which I believe will be required from any programmer
in the foreseeable future. Writing programs visually while leaving the tedious code
generation task to the machine, designing programs declaratively with the semantics of
the application domain, and talking with the code in a natural language written by the
programmer inside that code are just three domains of innovation the new generation of
programming languages should embrace.
In this regard, what makes the Ring language so important is that it naturally
emerged as a solution for these strong requirements when I tried to solve them in
existent programming languages and couldn’t. Every feature of the language finds its
origin in a problem I faced when I was working on the PWCT project,1 and every decision
I made on its design, even if it breaks the norm and sometimes shocks the common
sense, forms an answer to a practical problem that was unsolvable or hard to solve in
other languages. In fact, most of them focus restrictively on conventional programming
paradigms such as procedural, object-oriented, and functional programming. In Ring,
our message is to connect between those paradigms and others, such as declarative and
natural language programming, in a new programming experience.
1 http://doublesvsoop.sourceforge.net/
xxvi
The beauty of the book in your hands is that it embraces the culture of the language
while presenting its powerful features in a practical way. Mansour really shows how Ring
can be an option in developing small or large programs and in enabling a programming
journey based on prototyping, experimentation, iterative design, clean architecture, and
code craftsmanship. Nevertheless, the author, who I consider to be a master of Ring and
programming in general, has revealed the secrets of the great programmers in thinking,
analyzing, modeling, and writing code. Many technical constructs famously known
to be complex in programming, such as MVC architecture, data-oriented modeling,
scope management, function and object composition, event-driven systems, real-time
applications, and even game programming, are all introduced in an innovative visual
way, with tons of examples and many engaging stories to tell.
By contemplating the strategies and real-world situations in this instructive, well-
organized book and how they are solved nicely in Ring, I can say with confidence that
the message behind the language has been delivered. That said, I hope that this book
will help you gaining an emotional connection with Ring as a thinking tool, as a hackable
learning environment, and, ultimately, as a friendly programming language built
around the core values of syntax freedom, programmer’s responsibility, technological
transparency, and multiparadigm programming.
Welcome to Ring, and welcome to Beginning Ring Programming.
Mahmoud Samir Fayed
Creator of Ring
Egypt, January 2020
foreword