3 ms·
> Ruby doesn't come out of the box with the web app orientation of PHP, but then why choose Ruby in the first place? It was and is absolutely possible OOTB to
by lloeki 2y ago
> Ruby doesn't come out of the box with the web app orientation of PHP, but then why choose Ruby in the first place?
It was and is absolutely possible OOTB to do PHP-style webdev with Ruby quite trivially with the Ruby-included CGI and ERB (a.k.a eRuby; which has an `erb` command).
The set up is hardly any different from any other CGI setup be it with Perl or PHP.
Here's a `foo.rhtml`†:
% cgi = CGI.new
<%= cgi.http_header("status" => "OK", "type" => "text/html") %>
<html><body>
<ul>
<% [1, 2, 3].each do |a| %>
<li>
Hello <%= a %>
</li>
<% end %>
</ul>
<%= cgi.params.inspect %>
<%= cgi.path_info %>
<%= cgi.script_name %>
<%= cgi.request_method %>
</body><html>
You'd configure the HTTP server with calling `erb -r cgi <script>` for `*.rhtml` and... that's it.
So we mimic a CGI server call:
env REQUEST_METHOD=GET PATH_INFO=/foo SCRIPT_NAME=foo.rb QUERY_STRING='foo=1&bar=2' HTTP_HOST=foo.com erb -r cgi foo.rhtml
There was even an Apache `mod_ruby` for a time.
It just didn't make sense very quickly as soon as Rack came to be a thing††, and then you're one step away from Sinatra†††.
Even then you can easily still use pure Rack (or even Sinatra) and ERB in extremely PHP-esque ways.
† `.rhtml` is the conventional extension for ERB files that generate HTML, while `.erb` is the generic one.
†† Bonus is that you don't even have to setup a whole Apache/NGiNX server on your machine; you just spin up the Ruby-included WEBrick so it's even more beginner friendly.
††† Which contrary to Rails is hardly prescribing about anything.