Web "Hello World" in Ruby without RoR!

calendar_today agoschedule1 min read
— Originally published at dev.to

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.

1 Comment

0 votes
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelskiverified - Apr 23

Sovereign Intelligence: The Complete 25,000 Word Blueprint (Download)

Pocket Portfolio - Apr 1

Why Are There Only 13 DNS Root Servers For The Whole World? Is that a problem

richarddjarbeng - May 7
chevron_left
151 Points4 Badges
2Posts
0Comments
Software Engenier

Related Jobs

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!