Ruby web app lifecycle - ruby-on-rails

I am interesting in some manual/tutorial which I could not find.
What is a lifecycle of Ruby Web application? Ruby is a scripting language. Same as PHP. But as I heard, Ruby is precompiled before running. So the questions are:
when precompilation is done?
how precompilation is working for different files and classes?
what is a lifetime of precompiled code?
how route is passed to ruby application from apache/nginx (example could be made based on sinatra)?
maybe there is a tutorial on how to make own simple web framework, or web page without any web frameworks, but with pure ruby.
where is session object located?
etc.
Every manual contains only information on how to use some framework, but how it is working internally, nothing is describing this.
Thank you.

Ruby is "interpreted" just as PHP is, but this is also a form of compilation. What you might be confusing is that Ruby on Rails and Sinatra, as with most Ruby-based frameworks, are hosted as persistent processes. PHP tends to be interpreted for each request, though accelerators and web server modules do reduce the overhead of this process significantly.
I wouldn't suggest trying to make your own framework as this is a non-trivial task and even as an academic exercise would be of limited value. It's best to study something like Sinatra to see how that's implemented in order to learn more than to start from scratch.
Rails is quite well documented. If you have a specific question about a component of the framework that can't be answered by simply reading the source code, which is usually fairly well organized, you can post that here.

Related

Clarifications about Rails and Node.js

Up to now I've always used PHP with or without a framework but a month ago I decided to start something new: Ruby and Rails, I found them quite easy and similar to PHP and some PHP frameworks in how they works but using a simpler syntax and many other advantages.
Some days ago I started reading about Node.js, Node.js vs Rails, "why node.js is better"...
I'm a bit confused but my objective is to learn something modern that will not become obsolete in a few months so:
What are the main differences between Rails/Ruby and Node.js and a framework based on it like Express.js (except that one is written in JS and the other in Ruby)?
What are the main advantages/disadvantages of using Node.js and framework based on it instead of a Ruby based solution like Rails?
Thanks!
There aren't enough differences between Node.js and Rails for it to practically matter.
A lot of what Node.js can do can be pulled off in Rails with things like EventMachine and Pusher. So unless you are really familiar with Rails' limitations, and know you'll be pushing the boundaries, you'd be hard pressed to make something a seasoned Rails developer couldn't do.
Having built apps in Node and Express I can say that they alone aren't enough to make a sexy application. They can seem just as old and stale if you don't have an outstanding frontend UI to facilitate the backend possibilities. Instead of comparing backend servers, I think the real future of doing amazing things is in front-end JavaScript frameworks like Backbone.js that use Express/Rails/Node.js on the backend.
I have chosen to go in the direction of Backbone.js with Rails as my backend API server. Because it's so easy to rapidly create a very nice RESTful backend server in Rails. Rails also makes working with CoffeeScript and precompiling/organizing Backbone code a breeze. There are already decent Backbone.js gems out there for Rails.
The Rails core is also able to acknowledge and embrace the fact that frontend JS MVCs are logically a good next step, and they have been working to strengthen the bond between the two. For those same reasons they have also worked to make Rails an even better API server so that it can work with frontend JS easier. Node.js and Express aren't putting as much effort to coordinate with frontend JavaScript MVCs as the Rails community is.
Being good with a JavaScript frontend MVC and Rails as a backend makes you also great for both worlds in terms of getting a job. You will easily be able to hop onto a Node.js project and add value to that team with your superior frontend experience, and you can also roll with the punches on a Ruby on Rails team and add value to them as well.
As official Node.js website explains it:
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
On the other hand Ruby on Rails official website says:
Ruby on Rails is an open-source web framework that's optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration.
Given this I guess that it is more appropriate to compare Ruby and Node.js, but even this is not quite right given that Ruby is programming language and Node.js is NOT. You could probably compare JavaScript with Ruby but I guess that is not what you meant to ask with this question :)
So, for me, key point in understanding what Node.js truly tries to accomplish is well described on Node.js about page. Key Node.js idea (for me) is described in this sentences:
Node is similar in design to and influenced by systems like Ruby's Event Machine or Python's Twisted. Node takes the event model a bit further—it presents the event loop as a language construct instead of as a library. In other systems there is always a blocking call to start the event-loop. Typically one defines behavior through callbacks at the beginning of a script and at the end starts a server through a blocking call like EventMachine::run(). In Node there is no such start-the-event-loop call. Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform. This behavior is like browser javascript—the event loop is hidden from the user.
What this should enable you, is that you should be able to easily write highly concurrent programs without even thinking about concurrency using JavaScript syntax and callback functions as basic concurrent runnable units.
Your fear that either Rails or Node.js will be gone in a week is unfounded. Rails has a large community and will be around for a very long time even though currently (early 2012) it's getting a bit of hate thrown its way. Node.js is just getting started and has so much attention I don't think it will have any problems getting to the Rails level some day.
That said I've been evaluating Node.js and Rails as options for a project and the reasons I choose Node.js over Rails are:
"The Rails Way" - In my (admittedly limited) experience with Rails it really seems like you either do it the Rails way or you are going to be in for a world of pain. A big part of the Rails way is to use the ActiveRecord model. The advantage of this is that there are a lot of gems that work with your code happily because they know you'll be using ActiveRecord. The disadvantage is you are mixing your data access & model. I am not a fan of this idea so the Rails way for me still seems a bit.. off.
JavaScript is a key part of client side web development and the idea of using it on the client and server is interesting. I'm not super strong at JavaScript and I can't imagine a better way to get better then to have to use it everywhere.
My project has real time communication needs which while I'm sure can be done in Rails there seems to be quite a bit of positive mention on Nodes ability to handle this with socket.io being the front runner option.
At the end of the day no matter which you choose you will have a great time & learn a ton of new stuff that will change how you write code. If you're not on a big time crunch I'd recommend building a small project management tool in both and see which you prefer.
Either way.. Good Luck!
2 things - performance & productivity.
Performance (more details here)
(source: jslang.info)
Productivity (how fast you can build that app)
Ruby on Rails is specialized and highly productive tool for creating so called Web 1.0 and Web 2.0 applications (99% of internet sites are such apps). In my subjective judgement and experience in this area Rails about 2-4 times more productive than node.js or express.js.
For Web 3.0 apps (realtime things, client-side MVC, etc.) this isn't true, RoR doesn't keep its advantage there.
So choice depends on use case and priorities.
I know a lot more about Node.js than I do about Ruby. That being said, Ruby is much more widely adopted. It is currently a very hot skill to have in the work place. Some may argue with me on this but I think that Node.js is still "under development" and will be for a little bit longer. It has a lot of promise but just hasn't been adopted by many companies and projects yet.

Uses of Ruby on Rails

I am a somewhat experienced rubyist, but I am now starting to do rails development. I know rails is used to make "web applications" but what exactly does that mean? Do you build entire sites with rails or do you build a feature for a website in rails, and integrate it in to an existing website? (I know this question is kind of vague, but any answers to how ROR is used in the creation of websites would be much appreciated).
Generally speaking, you will build a full site with Rails, though you can integrate it into an existing website, as well. It is designed to provide a top-to-bottom solution for entire websites.
For standalone features or small scripts you just want to expose to the web, you might look at something like Sinatra or Camping.
What is Ruby on Rails?
I'm also build some rails applications in that I had entirely use rails to develop my web application. It's quite easy rather than using two or three languages. But when I developed I had to use Javascripts, CSS and Ajax to create more attractively
For me it's a great framework to build website and minimal knowledge is required although experience and knowledge of ruby gives you an upper hand.
I also feel that rails is good not only to build websites but to manage it.
Also the support community of rails is awesome and is complemented by guys like Ryan bates with their screen casts.
Also when we build a website using ror, we have the flexibility to concentrate on different features at different times or by different person.
This gets a great modular structure.
So you can design the basic functionality today and tell your colleague to get the views attractive using JS and friends without affecting your functionality.
Also the external support for rails (like gems and plug ins) is awesome and makes life heaven!
I would suggest that it's a great framework and the best feature is that it is a very easy to begin and learn but it always surprises you with new things.
Thus it allows you to learn a lot!
Hope it helps.
The website/web application distinction is made because there are many Rails apps in existence with no front end or 'website' to speak of. Using Rails, one could serve JSON amongst a cluster of compute nodes for data aggregation, or text to unix sockets for display on terminals. The uses for Rails are only limited by ones imagination, and you could even use Rails for single host interprocess communication if you desired. This might prove effective for a highly skilled rails programmer rather than learn a new tool to achieve similar results.

How do Rails and Jruby relate?

Background: I have a java library that is accessed by many developers - usually via java. However, a few devs hope to access this via a Ruby API. I am gravitating in the direction of Jruby in order to implement a Java-Ruby API.
Questions:
My main concern is that these developers will not be able to utilize the API in their current, Ruby on Rails development framework; but instead will have to operate within Jruby's rails environment. Is this going to be the case?
Perhaps I'm missing a connection between Jruby and Ruby on Rails. But it seems that you can either work in Rails, or work in Jruby's rails - are these technically two different development environments?
I would really appreciate any light someone could shed on this..
Edit: Forcing developers (actually clients to be more specific) to make large changes to their projects in order to accomodate for this API is not really an option. So, if their Rails apps would have to be converted to Jruby on Rails apps, then perhaps someone could offer yet another alternative for me (other than Jruby)?
First, I should mention that Ruby on Rails is simply Ruby code, and the same code can run in the JRuby environment (on the JVM) or the MRI (written in C) runtime.
However, only JRuby can load Java classes and use a Java API. Asking your clients to switch their deployment environment to JRuby may not be feasible, but if you can set up a service in Java that speaks XML or JSON then they will be able to easily use it from Rails.
You might find the answers to a similar question helpful. Depending on your application it may be possible to seamlessly port your RoR app to run on JRuby subject to the caveats noted in that answer. But you'd want to be sure that the benefits of doing so balanced the effort and potential risks.
Alternatively you might consider making these Java library facilities available to your MRI Rails app as an internal web service (which could be Rails or Sinatra or similar) built on JRuby. That's way you would decouple the elements and de-risk the project. If you subsequently were comfortable with that working you might then consider collapsing them back together, all under JRuby-on-Rails if that made sense.
UPDATE: Adding more detail at questioners request
As I'm not familiar with what your application or Java libraries do this will be somewhat abstract but I trust you can fill in the blanks to suit your needs.
It seems that full porting of your Rails apps to run on Jruby is not feasible. Fine. So one idea would be to expose your Java library as a web service (e.g. RESTful) to your RoR application. Let's call this thing JLS. In this case it would run as a Java or Jruby network service, listening for requests, call into the library and send back responses (general waving of hands here but you get the idea).
Depending on what your developers are more comfortable with you can create JLS in Java running a Servlet container maybe. Or you could develop JLS in Ruby, running on Jruby (binding to your Java library) and use a framework like Rails or Sinatra to implement it.
On the RoR side, assuming you've made JLS RESTful (and I don't know if that's even appropriate in your case), you can use the ActiveResource facility for communicating with JLS.
In summary the RoR app and JLS run in separate memory spaces (maybe even different hosts). Each can be developed somewhat independently (with RESTful interface agreement of course) and your RoR app does not need to be disrupted or put at risk.
This suggested option comes with tons of small print but I hope you get the general idea.

Ruby on Rails for web applications, Django for webpages?

Days ago I read something like "Ruby on Rails is for web applications, Django is for standard webpages". Is that true?
I have to decide in the next weeks if I go with Ruby on Rails or Django for an university project. It will be an email marketing software.
What do you advise me to use?
This is mainly because of their heritage - Rails was originally used for web applications like Basecamp, while Django was used to build newspaper/magazine sites.
I would say both have long since outgrown their original purposes however.
No that is not true, but however, Django has the built-in Admin back-end making it a great starting point for a CMS or something similar. Django is still very capable for creating complete web applications!
I presume this is:
Ruby on Rails (i.e. the Rails framework, using Ruby as the programming language)
vs
Django (i.e. the Django web framework, using Python as the programming language)
Both of these frameworks are Model View Controller (MVC) frameworks, so they are both capable of web applications and web pages.
Therefore, your decision is really "Python or Ruby".
Both Ruby and Python are object-oriented languages and are easy to get into... they both have quick-start guides here:
http://www.ruby-lang.org/en/documentation/quickstart/
http://wiki.python.org/moin/BeginnersGuide
I think Ruby has the edge in terms of simple documentation and ease of use - as long as you are happy to abide by the rules that the language enforces (which aren't necessarily a bad thing by the way).
Both frameworks are great for what you're trying to do and Ruby and Python are similar in a lot of ways. My suggestion would be to skim through the online documentation for each and go with what feels best for you.
First, you need to answer 2 questions:
Do you prefer Python or Ruby?
Do you need a minimalistic framework, or a more complete one?
As a minimalistic framework in Python, take a look at web.py.
Both are great frameworks.
The question is.. Do you prefer (or feel more comfortable, or know better) 'Python' or 'Ruby'?
Once you have the answer to this question you also have the answer to the original one.
You can use either framework to create almost any web application imaginable.
If you have no language preference, and you don't have a preference for their different design patterns (Django is nearly not as strict MVC as Rails), then think about the different Python and Ruby libraries/apis out there that could be useful for your project.
Go with whichever language supports those libraries/apis that help you the most.
In my experience, Rails developers are more likely to work in small, funded startups. Django seems to be the preferred environment for independent consultants building websites for small businesses. If you're picking a language to gain experience, you might keep this in mind.
You'll be spending most of your time configuring, learning the tools, frameworks, and environments. The language itself (Ruby or Python) will not be a big hurdle for you.
Also - Rails is really nice to develop with on a Mac. I don't know about Django tools.

what is ruby on rails?

I am a front end developer (HTML,CSS,JS & jQuery) I know a bit of PHP. I am trying to grasp what Ruby/Ruby on Rails is.
On http://rubyonrails.org/ it says "Ruby on Rails is an open-source web framework that's optimized for programmer happiness"
In actual, non-dreamy terms, what is special about Ruby/Ruby on Rails, and how can a person like myself benefit from learning to use it?
My simple question is: whose sister is it? PHP, mySQL, ASP, coldFusion, jsp, apache?
What does it do?
The Language
Ruby is a recent programming language that shares most of its heritage with Perl and Smalltalk. You can see what Ruby looks like (and try it yourself) at tryruby.org.
Just like you can use PHP to write web pages, you can do the same thing with Ruby.
The Framework
Ruby on Rails is a set of software devices that help you to more easily write a website in Ruby. The primary things they try to facilitate ("make easy") are:
Storing related data (e.g. blog posts and the comments on them) to a database.
Accepting web requests and respond to them programmatically (e.g. check a user's password)
Composing HTML using your data, with layouts and templates to make it easier.
"Rails," as it's called for short, is built with extreme prejudice towards certain application models, particularly MVC. What this means is that unlike PHP, where any .php source file is fair game for any bit of code, most code in a Rails application is written in a particular, conventional place. When people compare Rails to PHP, they often point this out.
There's a lot I didn't cover, but these are Rails' most basic features. To see what it looks like, I'd suggest watching the infamous, "Creating a Weblog..." screencast.
If you're looking for something to compare it to for someone coming from PHP, think of it like CakePHP for ruby. It's a bunch of pre-developed low-level classes and systems that can then be extended out to use in many different systems. It also bundles in a lot of other helpful tools and a plugin architecture. It uses ruby and an HTML templating language based on ruby (erb) rather than PHP.
It's a framework for Ruby whose sweet spot is CRUD based web apps. They make a number of assumptions (aka "conventions") that make it easy and fast to create tables with 1:1, 1:m, and m:n relationships, objects, controllers, and web pages for exposing and manipulating that data on the web.
Ruby is just another object-oriented language, like Java, C++, or C#. It was invented by a gentleman from Japan.
This might not be the best analogy, but like you would use HTML/CSS/JS to display a cohesive entity with specific stuff in their specific places. Rails provides a framework to allow you to work in an organized environment with specific stuff in their specific places. As well as having goodies to provide for more rapid development.
Generally:
Html would contain the content you want to display, while CSS would help control the specific look of that content, while JS would allow you to manipulate or interacte with said content.
Rails, uses MVC (Model, View, Controller) to provide a similar organization. Where Data objects are generally categorized as Models, while Views control the displaying of the Data. And Controllers would would facilitate the flow of that data from View to Model and vice versa.
Rails is not the only game in town. So do look around, as there are other great frameworks out there, and find the one best suited to your style and language needs.
Philosophy and design
Ruby on Rails is intended to emphasize Convention over Configuration (CoC), and the rapid development principle of Don't Repeat Yourself (DRY).
"Convention over Configuration" means a developer only needs to specify unconventional aspects of the application. For example, if there is a class Sale in the model, the corresponding table in the database is called sales by default. It is only if one deviates from this convention, such as calling the table "products sold", that the developer needs to write code regarding these names. Generally, this leads to less code and less repetition.
"Don't repeat yourself" means that information is located in a single, unambiguous place. For example, using the ActiveRecord module of Rails, the developer does not need to specify database column names in class definitions. Instead, Ruby on Rails can retrieve this information from the database based on the class name.
Taken from wikipedia
Ruby An elegant object-oriented programming language with objects like Smalltalk and syntax something like Python and Perl. It's kind of what PHP would have been had it been done right. That is, Ruby takes features from advanced "real" languages and combines them with the interpreted efficient syntax and library-rich practicality of Perl, Python, and PHP.
RailsAn advanced web framework. Many recent web frameworks are considered to be copies of or at least inspired by Rails. (Google "rails clone".) "The first good web framework".
Both Ruby and Rails are generally highly regarded.
Ruby is an object oriented programming language. Rails is a framework built in Ruby for developing web applications. I have been using rails for about 6 months now, and its been great so far. If you want some of the same features of rails, you should also consider django (python), cakephp or symfony (php).
Pros:
MVC Architecture - Code is organized,
broken up into small, manageable
files
Database abstraction - you dont have
to write SQL queries, database types
are easily interchanged
Convention over Configuration - rails automatically knows file, database, variable names without explicitly being specified
Fast Development - Rails requires less lines of code because of all the functions it automatically takes care of and is more expressive, resulting in faster development
Cons:
Slower - this is often debated, but a comparable rails application is slower than its PHP counterpart. Rails 3 is supposedly going to include speed increases.
Less Literature - rails lacks the plethora of tutorials, books and api that is available for PHP.
Less Hosting Support - Not all shared hosts support rails, but you can set it up on any vps or dedicated. Deployment is also a bit tougher in Rails as opposed to PHP
Ruby is a programming language. Ruby on Rails is a piece of software written in Ruby, it helps you develop web applications.
You can look at Ruby on Rails as if it is a set of tools. It helps you do common tasks when you're making a dynamic web application. E.g. session handling, cookies, database interaction, etc.
Rails goes beyond being just a library, though. To really understand this, you would have to experiment with it, which is actually very easy.
Rails Guides is a fantastic place to start.

Resources