-What are you, Morpheus?
The number of frameworks in the world is growing at an alarming rate. Thousands of them already at the moment. Fortunately, the best of the bottom - not so much. The whole world of Web developers is simply divided into several groups, which will give preference to one or another framework. I do not like blacks and holivars, so God bless him with what is best. I just want to talk to the ASP.NET developers about Ruby on Rails.
What for?
')
Tell your story. I've been writing ASP.NET since 2004. I know many corners of this system and I know that it is amazing in itself. I experienced 3 big projects on .NET and wrote from 10 sites, which now continue to work stably. If we talk about freiuknost, then .NET I was satisfied with everything. In the biggest projects I found solutions to the most difficult tasks. And so, after 6 years, I just wanted to see what else is on the market? Naturally, the first thing I came across was RoR.
The article is intended for experienced programmers. If you can not distinguish the compiler from the interpreter, then you better google.
What interested me? I looked at this
screencast . It smartly describes how you can create a blog in 15 minutes using RoR. Well, I decided, if you say that everything is so happy, you should try.
Lyrical digression. In order for the Windows owners to see the mov file without installing QuickTime, they will have to get an alternative version of Quick Time and install Media Player Classic . Most screencasts on ruby ​​are made on Mac OS. I myself - burned vinduzyatnik, and I had to tinker a bit. Again, this is not an article about holivarah.
Let's return to the topic.
Let's talk about terminology and what we need
Ruby is an interpreted high-level language for fast and easy object-oriented programming. The language has a multithreading implementation independent of the operating system, strict dynamic typing, garbage collector and many other features. Ruby is similar in terms of the syntax for Perl and Eiffel, and for the object-oriented approach in Smalltalk. Also some features of the language are taken from Python, Lisp, Dylan.
Any programmer who has been developing on .NET for a year is able to understand and quickly learn Ruby. Yes, and articles on this topic written quite a few.
We will not stop.
You can install Ruby on Windows using
One-Click InstallI note that unlike the usual% APPDATA% directory, everything related to Ruby is better to put on the C: drive or something like that. Simply, it will facilitate further work with the system.
I’ll say at once that Ruby without plugins is like an uncut ruby. Therefore, Gems was developed.
RubyGems is a package manager for the Ruby programming language that provides a standard format for Ruby programs and libraries (in a self-contained “gems” format), tools designed to easily manage the installation of “gems”, and a server to distribute them.
In fact, I can’t find at least some similar replacement for a normal package manager in .NET. The concept of .NET is that everyone makes an installer and distributes packages for .NET in a form that suits him. Ruby is much simpler and better. For example, there is a website facebook.com, which is gaining popularity in Russia. An API is written for facebook.com that can be used in its programs. Someone has already written a shell for this API. Fine! This is what we can use by typing the following command in PowerShell:
gem install facebooker
About what it means to type gem install facebooker. Just me, as for the avid vinduzyatniki, it always led to a stupor. For it was necessary to search for the executable file gems. All you need is in the folder C: \ Ruby \ bin
Immediately recommend to update this Gems to the latest version, because in the future it will be very useful.
PS C: \ Ruby \ bin> gem update --system
Just a little more terms, and you can start programming.
Ruby on Rails is a software framework written in the Ruby programming language. Ruby on Rails provides an architectural sample Model-View-Controller (model-view-controller) for web applications, and also provides integration with a web server and a database server.
Provides a homogeneous environment for the development of dynamic AJAX-interfaces, with processing requests and issuing data in controllers, reflecting the subject area in the database.
Ruby on Rails is open source software and is distributed under the MIT license.
And here is our hero! As a matter of fact, if you still don’t understand how Ruby on Rails differs from Ruby - let me offer you the following equation that will help you figure it out.
dotNET => 'ASP.NET Framework'
Ruby => 'Ruby on Rails'
I think it will be more understandable. And so, we have a language, we have a framework that allows you to work in this language, creating WEB applications. Just a little bit and you can start.
For, after we set up and set up the framework, we just need to start the server and check how it works. Here I can begin to talk about how to put on the Apache system, sqlLite or MySQL ... But it is too long and dreary. There are two options to make life easier:
1)
Instant Rails is an already configured server that includes Apache, Sequel, and Ruby with Rails. You unpack the archive on disk, run the batch file, it’s foul of it a couple of times and configures the Apache and everything your heart desires. Plus, the installation of all this happiness is extremely simple. Even DenWER compared to this joy will seem terribly confused. I note that this option is ideal for those developers who consider Notepad ++ as the best development environment.
2)
Aptana - this is needed for those who can not live without Visual Studio or Sharp Develop. This is already a full-fledged studio that allows you to write on RoR and makes life much easier for the programmer.
I warn you: the studio will need to be placed in a folder that does not contain Russian characters in the directory names. For some parts of the framework do not understand what Russian means. I recommend to put as close as possible to the disk.
The studio itself will offer to put Ruby, if earlier you did not. Also, Aptana will notice that you need Firefox 1.5 and higher for debbag javascript.
Everything at your choice. I will work with Uptana, as she made life easier. Immediately I say that before starting work you will need to update Gems, as described above.
For the lucky owners of Windows 7 - Aptana works stably only under Admin rights. What I recommend to you.
Well, it seems everything told, you can start the project.

In Aptan, this is all done by right-clicking the mouse in the Rails Explorer window. Once again, I note that the entire project is better placed as close as possible to the root of the disk and in folders without Russian characters.
If you are a good programmer, then you can tinker with the rails and run them yourself through the console, and not through the right mouse button.
PS C: \ Ruby \ bin> rails testblog
As a matter of fact, in essence, their teams are absolutely the same. Both of them will create the skeleton of the site that you will write. The difference is that Aptana also will launch a server for this business. By the way, if she does not do this, then you can still fix it by clicking Window> Show view> Servers
Well, now at the address
http: // localhost: 3000 / we have an absolutely unnecessary page, which is happy to say that everything works well for us. Although, in fact, on this page hangs a tutorial on how to properly make your first database and access to it. Let's follow it, I just add some data so that you don’t get confused at all.
And so, here we are creating a new project:

And so, we created it:

Just want to draw your attention to a couple of facts. Firstly, if you really always wrote only under .NET and never saw Apache, then know that the root of any site is the public folder. Marked in blue. Rails generously bestowed this folder with various error page stubs, index pages, and so on. Well, it is very convenient.
Just for future generations, I note that in Ruby much attention is paid to the automatic testing system. The test folder is marked in purple. Just know that testing your applications is really important, and remember that Ruby allows you to automate this routine well.
The main joy Rail is marked in red. This is the App folder, which contains the 3 most important folders - controllers, models and views.
As a matter of fact, MVC is the essence of all Rail. After all, the application is built on them. Let's build a small piece of this application.
If you do not find such a tab, click Window> Show View> Genarators

Now we will follow the example from
http: // localhost: 3000 / and generate a scaffold recipe title: string author: string description: text
Soooo. It seems to me here that the meaning starts to get lost. What is scaffold? Here, just with this and need to understand! In my opinion, scaffold is one of the greatest goodies of Rails. I have seen a lot of data access over the course of my life. I wrote two self-made frameworks that allowed you to manage data. I used the ASP.NET Entity Framework, but ... Scaffold struck me with its simplicity and power. Let's take it in order.
scaffold - scaffold; scaffold; gallows; the penalty
to go to / mount the scaffold - lay down on the chopping block; end life on the gallows
to bring to the scaffold - to bring to the gallows
to send to the scaffold - sentence to death
And here not without jokes. I do not know what the developers were thinking, but the translation of this word is alarming to me.
scaffold - scaffolding
The house was scaffolded to the very attics. - The forests were lined up to the attic of the house.
2) support, prop, carry (on yourself) load
3) place (products) on high stages (for drying, protection from animals)
That's much better. Scaffold is something that carries the burden. In this case, a scaffold is what takes on the functions of access to the database and completely deprives the programmer of all the torment.
What kind of team we scored?
Entity:
recipeField: type
title: string
author: string
description: text
And so, we created a new entity in the data access layer.
In the db folder, the migrate folder was created, in which a script appeared allowing you to migrate our entity to the final database.
class CreateRecipes <ActiveRecord :: Migration
def self.up
create_table: recipes do | t |
t.string: title
t.string: author
t.text: description
t.timestamps
end
end
def self.down
drop_table: recipes
end
end
This class was automatically created by the framework, and has two methods. Up and Down. These methods will be used to roll forward to the base, and roll back from the base. That is, if you make changes to your scaffold several times, several versions of the migration script will be created, which will consistently call the Up method. This allows you to keep your database under control and normally roll it onto the server's production.
Moreover, please note that this is not SQL. This means that at the moment we are absolutely BD-independent.
Please open config / database.yaml and look at its contents.
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db / development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set up this same as development or production.
test:
adapter: sqlite3
database: db / test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db / production.sqlite3
pool: 5
timeout: 5000
Really nice? You can even make different types of databases on different servers.
Okay. We have an entity, we have a description of the database, but so far we have no database structure itself for this entity. Therefore, we right-click on the project, select Rake> db> migrate
Some more theory. What is a rake?
Rake is a tool for automating the assembly of program code. It is similar to SCons, Make and Apache Ant, but has several differences. This tool is written in the Ruby programming language, and so on. Rakefiles (similar to Makefiles in the make utility) use Ruby syntax. The author of Rake is Jim Weirich.
Rake uses blocks of anonymous Ruby functions to define various tasks using Ruby syntax. It has a library of basic tasks, such as functions for file manipulation tasks and a library for deleting compiled files (the “cleanup” task). Like Make, Rake can also synthesize tasks based on templates (for example, automatically building a file compiling task based on file name patterns).
And so, what we did was - we forced rake to migrate our spherical entities in a vacuum to the database. It is very nice of him. It remains only to write the access code to this entity and everything, you can enjoy life.
But here lies the main joke of Rails. Scaffold is no fool. And not only provided us with the access code for the entity, but also wrote MVC for it. We can safely go to
localhost : 3001 / recipes and see what we have there. And we have a standard type of access to the database, which allows you to make major changes and work with the database. You can play around.
What happened? Where is the code and how does it work? Everything is in place!
Go to the App folder and see what lies in the models, controllers and views of your project. You already have everything you need to work with the entity. I want to note that in Rails a lot is tied to the names of variables and files. So, studying recipes_controller.rb you will find a lot of simple code that is not originally related to views, but the comments indicate which files the framework will refer to.
Well, now it is time to give the programmer all the code to his playful pens and let him play around.
After all the data that I have outlined, you can review the video file, which I cited as an example at the beginning of the article. It will be much more understandable.
For further study, I recommend:
A delightful six o'clock lecture at Berkeley. (Part one)A collection of more than two hundred screencasts on Ruby:
http://railscasts.com/For a much deeper understanding of what I have outlined above, I recommend this article
guides.rails.infoThank you Wikipedia for providing quotes.
Thank you for your attention. I hope this article will give you the opportunity to taste the delightful RoR.
This article is made specifically for Habr and only for Habr.