I am currently in the planning process of making a website, and I have
heard that Ruby is good to use, but I am a little confused as to what
Ruby is. As far as I can tell, it’s code that generates XML (I don’t
know if I’m right).
I’ve been reading a few tutorials, and have made no sense of them. What
parts of the website do you use Ruby for? I was presuming HTML was used
for the structure, and Ruby for certain features.
But what type of things would you use Ruby for within a website? And
would you use HTML for example, to create the structure, or is it all
done in Ruby?
Ruby is a general-purpose programming language. It can generate any
data you’d care to; it’s not specific to web programming as PHP was
intended, but you can indeed have a process running on the server,
written in ruby, that serves HTML pages, either static or dynamically
generated.
Ruby is a general purpose programming language. It has no specific
task, but you can handle
almost every task with it. No more, no less.
So essentially, you can use Ruby for everything you could use Java/
Python/PHP for. Ruby has
some really nice frameworks for web development, Glen already gave you
the links.
But what type of things would you use Ruby for within a website? And
would you use HTML for example, to create the structure, or is it all
done in Ruby?
Well, it really depends on what your website is intended to do and
what Ruby technology/framework you choose.
For example, in something like Rails the site structure emerges from
the Ruby (controller) classes and the content of the pages would be
built from HTML with embedded Ruby expressions. (Of course, I have
glossed over a lot with this simplistic description.) So, for the
sake of argument, say your website tracks cars and their drivers and
you are using something Rails-like.
You could create the following Ruby controller classes: CarsController
and DriversController
In CarsController, define methods like new and list
Then you could have URLs like
/cars/list
/cars/new
Each method could have a corresponding HTML snippet (new.rhtml and
list.rhtml)
list.rhtml may look like:
<% cars.each do |car| %>
But what type of things would you use Ruby for within a website? And
would you use HTML for example, to create the structure, or is it all
done in Ruby?
Well, you can do everything in Ruby that can be done with a programming
language, including writing a webserver from scratch…
Now, the common usage is :
templating (ala PHP / ASP - what you suggest above, a mix and match of
Ruby code and HTML code),
database connections (to use with the templates),
user authentication modules,
interfacing with mail servers,
miscellaneous content generation (e.g. generating images or PDF on the
fly).
A web framework like Rails offers to do all of the above ; some are
lighter, and some applications can be designed to work with other
systems (like user authentication modules that could be called directly
from any kind of web application).
But what type of things would you use Ruby for within a website? And
would you use HTML for example, to create the structure, or is it all
done in Ruby?
To develop a project of any significant size, you’ll probably want to
look into
one of the many available ruby web frameworks, as others have mentioned.
However it is also possible to start very simply. Here is a “hello
world”
web program in ruby, using the built-in CGI module:
As can be seen from the source code shown on the page, the HTML is
generated dynamically using cgi methods.
Here’s a more real world example of using the CGI module. This is a
program that queries a bunch of online game servers and displays info
about which players are currently connected to which servers:
Again, we see all the HTML is dynamically generated within the program
itself. This inline approach is often OK for small programs like this,
but
if one were writing larger web programs, it’s preferable to separate the
HTML from the code. This is usually accomplished with a templating
system, which you’ll be introduced to when you begin exploring the
various ruby web frameworks available.
Hope this helps,
Bill
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.