strange result from isDigit() builtin function

74 views
Skip to first unread message

Irwin Rodriguez

unread,
Oct 1, 2023, 3:20:35 PM10/1/23
to The Ring Programming Language

How is this possible? the result is 1 (true)


 ch = ''

? isDigit(ch)



Mansour Ayouni

unread,
Oct 1, 2023, 4:26:37 PM10/1/23
to Irwin Rodriguez, The Ring Programming Language
Hello Irwin,

This is my explanation...

In Ring:
  • "" is same as Null,
  • and Null is False,
  • and FALSE = 0
Let's check that:

? "" = 0
#--> 1 (TRUE)

Hence, when you write ? isDigit(""), it's the same as ? isDigit(NULL), which is the same as ? isDigit(FALSE), which is the same as isDigit(0).

Obviously, ? isDigit(0) must return TRUE.

Other than that, isDigit() accepts also a number in string and says that is a digit. Try this:

? isDigit("2")
#--> 1 (TRUE)

Also, when you put a number in a string, not just one digit, it says it's a digit!

? isDigit("215")
#--> 1 (TRUE)


I don't know if this is a good or bad thing, and if Mahmoud will prevse the current behaviour of isDigit() for a good reason, or introduce a potential fix...

But in SoftanzaLib, I tried to avoid any confusion regarding the situation, by helping the programmer to be explicit about what he wants to check.

I crafted these lines of code to illustrate my point:

image.png

Hope it was helpful.

All the best,
Mansour

--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/350b0ce1-152f-4c4b-bf9a-b6272e20ebfan%40googlegroups.com.

Outnull

unread,
Mar 9, 2024, 5:18:25 PM3/9/24
to The Ring Programming Language
Greetings,

Upon observing this in my own code, I perceive a potential issue. If not an actual bug, the functionality might be confusing enough to warrant reconsideration. In the given instance, 'ch' seems to appear as an empty string, and typically wouldn't be regarded as a digit in any other language. Maintaining the current implementation of this function will inadvertently lead to bugs in one's application.

Hopefully the team reconsiders how this works.

Regards,
ON

Ilir Liburn

unread,
Mar 9, 2024, 5:38:36 PM3/9/24
to The Ring Programming Language
Hello ON,

I second that, but qualifies as a bug. Documentation states:  We can test a character or a string using the IsDigit() Function

Empty string doesn't qualifies as a string value, it have special meaning: NULL. Furthermore:

input = "0012500-PRY-09"
if ! isDigit("0012500-PRY-09")
input = number(input)
ok

? input

We convert string to number if it is not a digit? How that can be?

Greetings,
Ilir

Mahmoud Fayed

unread,
Mar 9, 2024, 5:47:07 PM3/9/24
to The Ring Programming Language
Hello Irwin, Mansour, Outnull, Ilir

Thanks for the bug report :D

This fix update isDigit() and related functions like isalnum(), isalpha(), etc.
All of these functions in Ring 1.20 will return False if the input is an empty string.

Greetings,
Mahmoud 

Ilir Liburn

unread,
Mar 9, 2024, 5:52:03 PM3/9/24
to The Ring Programming Language
Hello Mahmoud,

and what about

input = "0012500-PRY-09"
if ! isDigit("0012500-PRY-09")
input = number(input)
ok

? input

Greetings,
Ilir

Mahmoud Fayed

unread,
Mar 9, 2024, 5:57:47 PM3/9/24
to The Ring Programming Language
Hello Ilir

>> "and what about"

In the last two days I revised the all of the behaviour of operators & implicit conversion
So we get a runtime error when we convert a String to a Number and this string doesn't have a valid number, i.e. "0012500-PRY-09"
This is done for all of the operators like +, -, *, /, %, etc.
An exception is (= and !=) because we want True/False output (Not a convertion/error)

With respect to the Number() function, it's the last thing that I need to revise to finish this issue.
But what I am going to do will be
Number("0012500-PRY-09") ----> RUNTIME ERROR

Summary: 
(1) Ring will still provide implicit conversion for strings that have valid numbers but will produce a runtime error if the string doesn't contain a valid number.
(2) Using (=) and (!=) will always produce True/False ---> No Runtime Errors (As expected) 
Also ? "0012500-PRY-09" = 12500 ---> FALSE
(3) Using And/Or/Not with Strings/Numbers/Lists/Objects will always produce a Logical Value (True/False)

Greetings,
Mahmoud

Ilir Liburn

unread,
Mar 9, 2024, 6:05:05 PM3/9/24
to The Ring Programming Language
Hello Mahmoud,

that makes sense, only question is how (and does) it effects existing Ring code.

Greetings,
Ilir

Mahmoud Fayed

unread,
Mar 9, 2024, 6:11:39 PM3/9/24
to The Ring Programming Language
Hello Ilir

>> "that makes sense, only question is how (and does) it effects existing Ring code."

I am optimistic, everything should be fine, since we are changing the behaviour to be better (still implicit conversion but doesn't hide the bug when the string contains invalid numbers).

Greetings,
Mahmoud

Mansour Ayouni

unread,
Mar 10, 2024, 5:37:14 AM3/10/24
to Mahmoud Fayed, The Ring Programming Language
Hello Mahmoud, Ilir and All,

I also think it will be fine. Still, we need to double check it, especially for any unexpected behavior.

All the best,
Mansour

--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages