Is it possible to make a web application in ruby without using a frame?
Too long; Do not read
Yes, it is definitely possible. Most Ruby infrastructures are built using pure Ruby on top of other middleware libraries, such as web server interfaces.
Ruby and the web
Ruby is a general-purpose language; therefore, it is not specifically designed for web development. For example, PHP is a language that was written to create web applications. In Ruby, you'll need some libraries to properly handle HTTP headers and web-specific elements.
In Python, for example, (another common programming language), we have a standard specification for the web server interface called WSGI (Web Server Gateway Interface). Each server that implements the WSGI specification is called WSGI-compliant. And any WSGI-compatible server can run the same WSGI Python script in the same way.
Why am I telling you this when I talk about Ruby? Because Ruby has a very similar WSGI concept, with the possible exception that it is not yet a standard. Its name is Rack , and it provides an interface for working with a regular low-level HTTP / server that you don’t want to handle on your own so that we can use Ruby as if we were using PHP.
Ruby, Rack, and Apache
Take a real life example: Apache. Apache is one of the most common web servers. How does PHP work with Apache? Using mod_php . How are Python WSGI applications compatible with Apache? Using mod_wsgi . How are Ruby Rack apps compatible with Apache? Using mod_rack . Do you see a sample here? The web server needs to know how to properly associate your application with the request / response web context.
Rack example
Without addressing this abstract talk, let's focus on an example:
class HelloWorld def call(env) [200, {"Content-Type" => "text/plain"}, ["Hello world!"]] end end
This example is provided by the Rack website, and it explains how a Rack-compatible script works:
- You install
rack on your web server (you will find many Google manuals related to your web server). - You create the
config.ru file in the root folder ( .ru basically Ruby) - Paste this script
- You start it using the
run method
Et voilà, we have a web server interface. The env hash contains the environment variable from the current request, and the returned array contains 3 components:
- Response Status . 200 means "Everything is in order." 404 means "Not found." And so on.
- HTTP header . They describe the body of the response. Its length. Its contents. And so on.
- Body response . This contains the actual result of your application. HTML, JSON, XML, HXML, plain text ... whatever.
In PHP, for example, all this is done automatically. When you do echo "Hello"; , the response status and headers are generated for you by the PHP interpreter.
Alternatives Note
You can dig out everything you want in this field, but most of the technologies listed below are either outdated, or their use is very discouraged by the community.
In the early years of the network, there was a common interface used to run Perl, Python, Ruby, C scripts on the server side: CGI or a common gateway interface. This is an interface that can be used by almost any programming language, but is usually considered slow.
Some thought to revive this interface, making it faster. And from this, guess what came FCGI or FastCGI. This technology is used more often than you think. Some PHP scripts are sometimes converted to FCGI scripts to make them work faster. I do not want to go further on this topic, as there are many other links.
And finally, you can create your own web server. In fact, you do not need to create a web server with Ruby in order to use Ruby. Theoretically, a web server is as simple as:
- Listen to the port (80 times) until prompted
- Request transaction
- Print answer
- goto 1
In a real environment, you don’t want to implement your own web server to create websites. Therefore, I definitely discourage this.
And if so, why the framework chosen by most ruby web developers?
Pros
Frames are used to quickly develop your site. If you have deadlines, you don’t want to deal with low-level materials, and you will like the framework build -blog , which will manage as many boring things for you as you can and let you focus on the actual design of the applications.
Frames are usually open source and have large communities, which helps to quickly make the structure. You can easily understand that an error in the code detected by 10,000 pairs of eyes was found 10,000 times faster than the code that you write for yourself.
Ends
Some structures may be too large for your needs, while others may be too small. In the context of Ruby, there are huge Rails and his little brother Sinatra. One of them is huge, and the other is very small and really gets out of your way. Sometimes you need something in between.
Frames are usually very general. This means that you need to tweak things that seem obvious to your context.
Frames contain more code than you need. This is a fact that you can deduce yourself. This usually means more complexity and a direct amount of errors (although this is offset by the huge community around them).