Crazy Code using Ring - Part (8) - Mixing nested anonymous functions, Eval() & BraceEnd()

49 views
Skip to first unread message

Mahmoud Fayed

unread,
May 5, 2026, 3:23:21 PM (10 days ago) May 5
to The Ring Programming Language
Hello

Check the next sample, it contains multiple lessons/ideas
It's written in this way to introduce & mix these ideas together in one sample
i.e. it's not a guide to mix these features together (just learn about them and use what you need)
Features:
* Nested anonymous functions
* Calling anonymous function as a method
* Using Eval()
* Using BraceEnd() & avoiding the infinite BraceEnd() trap

$Server = new Server { host = "http://0.0.0.0" port = 3000 }

// We don't need to use new to create the object because it's already created
$Server {

    // We access host and port attributes from our anonymous function

    @get( "/", func {
        ? "Server running on " + host + ":" + port
    })

} // This will call braceEnd() method

class Server

    host port
   
    myfunc       = NULL
    lRunBraceEnd = True
 
    func @get cURL,fFunc

        // myfunc will be another anonymous function that call our anonymous function as method
        // Since myfunc is a string (contains the function name)
        // It's easy to pass it to C functions and call it from C libraries
        // I.e. we created a wrapper so our original anonymous function act as a $Server method
        // This avoid passing the object to the function or using $server.host to access the host attribute

        myfunc = Eval(`return func {
            vFunc = "`+fFunc+`"
            $Server { call { vFunc() } }
            }
        `)

    func braceEnd

        // braceEnd could be a trap if we are not careful
        // because we call myfunc() which access $Server using braces which call braceEnd() again
        // To avoid the infinite loop we use the lRunBraceEnd attribute

        if myfunc
            if lRunBraceEnd = True
                lRunBraceEnd = False
                call myfunc()
                lRunBraceEnd = True
            ok
        ok



Greetings,
Mahmoud
Reply all
Reply to author
Forward
0 new messages