Ruby without Rails

Search for a command to run...

No comments yet. Be the first to comment.
Remember my post about switching to VSCode? Well, plot twist: the very next day after publishing it, I switched to WebStorm. And guess what? I've been on JetBrains IDEs ever since. I know, I know. Another "why I switched editors" post. But hear me ou...

I had been doing some basic interpreter work in Go and wanted to redo it in another language to solidify my understanding. Naturally, I chose Odin, it’s another great language, with a Go-like, easy-to-approach syntax and no unnecessary complexity. I ...

Why Emacs Keybindings? I've always preferred Emacs-style editing, especially the way it handles cursor movement and text manipulation. Even though I have switched to using VSCode for most of my development work, I often found myself missing the intui...

In my previous post about building C/C++ projects with CMake, I shared a simple CMakeLists.txt setup for compiling a basic project. Today, I’ll dive into a specific need that many developers encounter: linking libraries directly from project folders ...

Some time ago, I wrote a post about using Vim to look cool and why that’s not a good idea. Now, funny enough, here I am—having switched to VSCode after years of hopping between Vim and Emacs. This isn’t one of those "Vim sucks, I'm going back to VSCo...

Ruby on Rails is a great web framework that powers awesome websites like Shopify, GitHub, Basecamp, HEY.com, and more.
This blog post is not to say that Rails is bad or anything like that.
I recently got really inspired to give Ruby a real try after watching an interview and some talks by DHH, the creator of Ruby on Rails. In those talks, he shares how much joy he finds in programming with Ruby. I can totally relate to things he says, like “a text-editor-friendly programming language (no IDE required)” and “programming for the sheer joy of programming”, etc.
So, I wanted to give Ruby a real shot and started the “Try Ruby” tutorial on the official Ruby website. Midway through the tutorial, I got curious about what it would take to do some web programming with Ruby. I ended up discovering how to build a simple web server using Ruby. Below is the simplest web server implementation in Ruby, so future me won’t have to ask some AI about this again.
require 'webrick'
# Define the server and specify the port
server = WEBrick::HTTPServer.new(Port: 3000)
# Define a main route
server.mount_proc '/' do |req, res|
res.body = 'Hello, world!'
end
# Ctrl+C to shut down the server cleanly
trap 'INT' do
server.shutdown
end
# Start the server
server.start
require 'webrick'
require 'erb'
server = WEBrick::HTTPServer.new(Port: 3000)
# Define an ERB template
template = <<-HTML
<html>
<body>
<h1>My name is <%= name %>!</h1>
<p>The current time is: <%= Time.now %></p>
</body>
</html>
HTML
server.mount_proc '/' do |req, res|
# Define variables for the ERB template
name = "Bmax"
# Render the ERB template with variables
renderer = ERB.new(template)
res.body = renderer.result(binding)
# Specify content type as HTML
res.content_type = 'text/html'
end
trap 'INT' do
server.shutdown
end
server.start
# server.rb
require 'webrick'
require 'erb'
server = WEBrick::HTTPServer.new(Port: 3000)
index = File.read('index.html')
server.mount_proc '/' do |req, res|
name = "Bmax"
renderer = ERB.new(index)
res.body = renderer.result(binding)
res.content_type = 'text/html'
end
trap 'INT' do
server.shutdown
end
server.start
<!--index.erb-->
<html>
<body>
<h1>My name is <%= name %>!</h1>
<p>The current time is: <%= Time.now %></p>
</body>
</html>