DEV Community

luiz filipe neves
luiz filipe neves

Posted on • Updated on

Web "Hello World" in Ruby without RoR!

If you want to understand how things work beyond what's under the hood, the first good step is to try to write something from scratch. So, in this post, I will show you how to write a 'hello world' app for the web in pure Ruby. Above, you'll find the simple 'Hello World' code example.

class MyApp
 def say_hello
   "Hello World"
 end
end
MyApp.new.say_hello
Enter fullscreen mode Exit fullscreen mode

Now we need to turn this code into a Rack app. Rack is an interface for developing web applications in Ruby. The structure of a Rack app looks like this:

run do |env|
  [200, {}, ["Hello World"]]
end
Enter fullscreen mode Exit fullscreen mode

In this case, the code works like this:

class MyApp
 def say_hello
   "Hello World"
 end
end

myapp = proc do |env|
  [200, {'Content-Type' => 'text/plain'}, [MyApp.new.say_hello]]
end
run myapp
Enter fullscreen mode Exit fullscreen mode

Run this using the rackup gem or another supported web server.(read more here)

rackup myapp.ru
--> Puma starting in single mode...
--> * Version 4.2.0 (ruby 2.6.5-p114), codename: Distant Airhorns
--> * Min threads: 0, max threads: 16
--> * Environment: development
--> * Listening on tcp://127.0.0.1:9292
--> * Listening on tcp://[::1]:9292
--> Use Ctrl-C to stop
Enter fullscreen mode Exit fullscreen mode

You can access the app by typing in your browser. 127.0.0.1:9292.

Top comments (3)

Collapse
 
vulong22121999 profile image
Long Vu

[200, {'Content-Type' => 'text/plain'}, [MyApp.new.say_hello]]

Collapse
 
luizfilipecosta profile image
luiz filipe neves

Thanks, i will correct the post.

Collapse
 
vulong22121999 profile image
Long Vu

Can't run :(