Module: LinuxStat::Misc

Defined in:
ext/misc/integer/integer?.c

Class Method Summary collapse

Class Method Details

.integer?(val) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'ext/misc/integer/integer?.c', line 18

VALUE isNumber(VALUE obj, VALUE val) {
  // Expecting a String as input, return Qnil for any other type
  if (!RB_TYPE_P(val, T_STRING))
    return Qnil;

  char *str = StringValuePtr(val);
  size_t len = RSTRING_LEN(val);

  // If the string is empty, return false
  if (len == 0) return Qfalse;

  size_t i = 0;
  char ch = str[0];

  // If the string starts with '-', skip it but ensure there are digits after it
  if (ch == '-') {
    i = 1;
    if (i == len) return Qfalse;
  }

  // Iterate through each character to check if it's a digit
  for (; i < len; i++) {
    ch = str[i];

    if (ch < '0' || ch > '9') {
      return Qfalse;
    }
  }

  return Qtrue;
}