Skip to main content

Data Types in Ring

  • Chapter
  • First Online:
Beginning Ring Programming
  • 429 Accesses

Abstract

In the course of my career as a programmer and a manager of programming teams, I have observed a common symptom of low-quality code: poor mastery of data type systems. Data types are the foundation of any programming language. They are part of a “language inside the language,” and they define the nomenclature of what is possible to say in the language and the range of what is possible to do through its features. Unfortunately, data types are not deeply addressed in the majority of programming books and documentation. Decidedly, that won’t be the case of this Ring book.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 54.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 69.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    This is not an abuse of language but a technical reality so common in computer language study and compiler design theory that it is well understood by programming language designers.

  2. 2.

    Call them features if you want.

  3. 3.

    In music, the Gang of Four is an English post-punk group formed in 1976 (the same age as me, and I particularly love their new single “Change the Locks”). In computer science, the Gang of Four are the authors of the ultimate reference book in design patterns. In Ring, the Gang of Four is composed of…well…read the section.

  4. 4.

    This is just an overview of the Ring features for every type. You’ll get a more elaborate idea about every type in its dedicated section of this chapter.

  5. 5.

    In reality, they are five, but we won’t discuss the Object C type in this beginner’s book.

  6. 6.

    Don’t be surprised when Ring gives you more than one option to do the same thing. This is done for a good reason: flexibility. Usually, I use func to define a function and def to define a method inside an object.

  7. 7.

    This level of control is unique in Ring. You can change everything, including the standard keywords of the language (using the ChangeRingKeyword instruction), the default values of the main logic arguments (true and false), and many other things. Some people would say this is risky, but if you adopt the Ring mindset, this is the spirit of the language made with extreme freedom and responsibility. Many applications become possible: crafting your own programming language with your own syntax and programming style without learning how to fight with the compiler theory, writing serious code in your native human language, explaining the internals of programming languages to your students by looking transparently at what happens under the hood (only Ring allows such a transparency), to cite just a few! But this should be used with responsibility to protect your program from unwanted critical threats.

  8. 8.

    Seriously, in the context of a web application, this can be a target for a seasoned hacker to break the logic of your program by stimulating a code injection attack. In this case, the solution is easy, as you’ll discover at the beginning of the next code snippet.

  9. 9.

    Scary Maze is a highly stimulating, exciting game requiring deep coordination between the player’s eyes and hands. To complete the game, you have to pass many difficult levels, so you need to be careful, skillful, focused, and patient.

  10. 10.

    It isn’t really a type, since there are only four in Ring.

  11. 11.

    Whatever form it takes (NULL or Null or nULL, etc.), remember that Ring is not case sensitive. Note that this particular feature is useful to identify the NULL values extracted from a database.

  12. 12.

    This particular case should be reported to the Ring team. In my opinion, this is a situation that needs to be covered, so that the isNull() function, when the NULL variable itself has been shifted from its default value (the empty string), becomes able to say something about it. I’ve published the code in the Google Group, so join the discussion at https://tinyurl.com/ryj9awc.

    In particular, you will discover in Mahmoud’s answer an important feature that was totally new to me: all the standard functions that are natively written in C and used by Ring, such as isNull(), for example, can be redefined in your program using pure Ring code. Hence, any behavior you want can be implemented!

    In our current case, I would add a new function called isNull() that reacts to all the cases I need, including the crazy case of putting any string in the NULL variable. In such case, I want the function to reject any craziness by ignoring any value other than the default empty string, as shown here:

      NULL = “anything”   if isNull(NULL) { ?”It’s null” else? “It’s not null” }   func isNULL(value)         if value = NULL or value = “” or value = “NULL”            return true         else            return false         ok

    Test it, and you will find that isNull() is no longer confused. NULL (whatever value it contains), an empty ””, ”NULL”, or a “crazy” thing will always behave as NOTHING. Satisfied? Use it. If not, change the function to cope with your needs.

  13. 13.

    Timbuktu is located on the southern edge of the Sahara nine miles north of the main channel of the River Niger. The town is surrounded by sand dunes, and the streets are covered in sand. The annual flood of the Niger River is a result of the heavy rainfall in the headwaters of the Niger and Bani rivers in Guinea and northern Ivory Coast. —From Wikipedia

  14. 14.

    Don’t pay a lot of attention to the first four lines for now. We will talk about them later.

  15. 15.

    Remember, we will always stay loyal to our precious principle of separation of concerns that was introduced in Chapter 1.

  16. 16.

    A reference architecture is a formal, consistent, and stable representation of the components of any system and how they interact. It is used mainly to discuss the system and explain both its static and dynamic aspects (i.e., its structure and behavior).

  17. 17.

    Operator overloading will be explained in detail in Chapter 7. So, don’t take the burden of asking what they are now.

  18. 18.

    In this case, the compiler of your language would help you check the correctness of your program by raising a large number of type errors. But what you would lose is the world of flexibility and expressiveness made possible with dynamically typed languages like Ring. So, please don’t leave this chapter before being confident you have the necessary understanding and have played with the several examples I provided.

  19. 19.

    http://ring-lang.sourceforge.net/doc1.10/checkandconvert.html

  20. 20.

    It would be better if this function were called str2number() and its inverse were called number2str().

  21. 21.

    This function can be easily implemented in Ring through an algorithm that iterates over the string items, checks whether the item is a number, and if so, adds it to a tempo variable containing the result.

  22. 22.

    Just replace exchangerate with er.

  23. 23.

    If your computer does not enable Arabic, then it is easy to find how to do it on the Internet depending on you operating system. These are some links that could help: if you are on Windows, visit https://tinyurl.com/wja6t3b; if you are on macOS, visit https://tinyurl.com/sye26xr; and if you are on Linux, visit https://tinyurl.com/d6uadk.

  24. 24.

    The two graphemes ك and represent the same letter K in Arabic.

  25. 25.

    Unicode is the de facto standard of representing text universally on any language and any platform. By the way, this is a subject every serious programmer needs to understand. I suggest you pay attention to what Joel Spolsky says about it in his blog Joel on Software (https://bit.ly/2ibBNoI).

  26. 26.

    In fact, Qt is a huge library for more than just graphic user interfaces. The GuiLib library is a complete hub between Qt and Ring. This means you can do everything Qt is capable of using only the Ring language.

  27. 27.

    If you put the new lines after the class code, an error is raised. You must organize your Ring programs in this order: load and import commands, main function (if any), other functions, and finally classes. You will learn more about structuring Ring programs in Chapter 6.

  28. 28.

    Use the Google Translate service from English to Arabic to get “,” the translation of book in Arabic (and to the Panjabi and Kannada languages we will be using on the next page).

  29. 29.

    Operator overloading means changing the meaning of an existing operator in the language (for example, the + operator) by a new implementation method defined by the programmer inside a user-defined class (for example, ucdString + “something” will change the + operator with the QString.append() function).

  30. 30.

    Don’t be shy. I ask dozens of similar questions every day. And if you check out my activity in the Ring Google Group (https://bit.ly/2WnFDmp), you will find plenty of them.

  31. 31.

    The wide majority of programming languages, being statically or dynamically typed, adopt this static strategy to define the scope of variables.

  32. 32.

    https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping

  33. 33.

    I suggest you want this excellent five-minute video presentation by Kevin Lych to get a quick idea of the data types supported in C: https://www.youtube.com/watch?v=V1mBtAZxHgw.

  34. 34.

    The following paragraph is extracted as is from a discussion between the author and the language creator.

  35. 35.

    Namely, the unsigned() function.

  36. 36.

    I particularly love this syntax sugar: n = 12590112 + 2 can be written as n = 12_590_112 + 2.

  37. 37.

    This is from the Ring documentation (https://bit.ly/2ET6QCT).

  38. 38.

    You will find the random() function useful in Chapter X when we discuss game programming in Ring.

  39. 39.

    http://ring-lang.sourceforge.net/doc1.10/mathfunc.html

  40. 40.

    Yes, extra spaces are intentional so we can apply the trim() function on them.

  41. 41.

    You can learn more about this in Chapter 7.

  42. 42.

    If at the end of your function you say return [r1,r2,r3], then in the caller code you can obtain three values (r1, r2, and r3) returned in one list.

  43. 43.

    http://ring-lang.sourceforge.net/doc1.10/lists.html?highlight=list

  44. 44.

    https://amzn.to/2WosCo0

  45. 45.

    I am not a believer that a programming language can be better or worse than any other. But for sure, I have met in the course of my career programmers of all kinds.

  46. 46.

    https://bit.ly/2QPjbwx

  47. 47.

    To be fair, Laurence taught me, for the first time, that dynamic languages influenced their static sisters in adopting innovations such as garbage collection, if-then loops, just-in-time compilation, and many more.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Mansour Ayouni

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Ayouni, M. (2020). Data Types in Ring. In: Beginning Ring Programming. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5833-0_2

Download citation

Publish with us

Policies and ethics