Breaking Down the Syntax

Now that we know how to build a minimal Cuba application, let’s take a deeper look at the syntax. We can split this example into threeparts:First, we require Cuba to load the gem and get access to its functionality.

require “cuba”

Then we can identify four methods that appear in most Cuba applications:

define

,

on

,

root

 and

res

.

Cuba.definedo on rootdo res.write(“Hello, Cuba!”) endend


ADDownload to read ad-free.


define

 allows us to create an application through a block.

on

 executes a given block if the passed conditions evaluate to

true

.

root

 returns

true

 if the accessed path is the root of the application (

"/"

 or

""
).
res

 handles the server response. In this case, we use the

write

 method to set the response body with the greeting message.Finally, the last line connects our application with Rack.

run(Cuba)

Don’t worry if you don’t understand what Rack is just yet, we’ll discuss it in the next section


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *