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
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
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
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
You can access the app by typing in your browser. 127.0.0.1:9292.
Top comments (3)
[200, {'Content-Type' => 'text/plain'}, [MyApp.new.say_hello]]
Thanks, i will correct the post.
Can't run :(