Continuing to learn Ruby? Yes sir!
In the
first part, we got an overview of Ruby and Rails and, I hope, decided whether we need it or not. Since we took up the language, it is necessary to fundamentally equip a
working training place. We need: the Ruby interpreter itself, the Rails framework, the database (MySQL, I think, will be the best choice) and the development environment itself, which should make the programming process enjoyable, convenient and fun;)
')
Development Environment
As in other interpreted languages, you can also create ready-made programs in Notepad, however, we seriously grabbed Ruby;) It would probably be worthwhile to google the IDE for the language, to do some kind of performance and functionality testing. But we are nonprofessionals, so we are looking for the most versatile, reliable, high-quality, proven solution. And it is: it is
Ruby in Steel for Microsoft Visual Studio 2005/2008. Yes, this is a purely commercial, closed product only for Windows. But, firstly, it’s a familiar IDE that will faithfully serve you in the future, secondly it’s not based on Java, which has a positive effect on the speed of work (this is important for weak computers like mine), thirdly, a full-featured trial -version works 60 days, which, I hope, will be enough for our course.
If you prefer other operating systems, you should definitely take a look at the free
NetBeans IDE 6.0 Ruby cross-platform.
The development environment you chose does not matter much in the future, but I will cover the process of installing components for Ruby in Steel - it is quite simple, since you can download
All-in-One Installer - a ready-made package with free Visual Studio 2008 and everything you need for our training place.
After downloading, unpack the archive and run Setup. Mark the necessary packages (in most cases you should leave everything by default), click Install and only have time to click Next and Finish in the installer windows that appear. During the installation, the latest Rails will be downloaded and installed, so the console windows are not closed and we are waiting for “Press any key to continue ...” to appear. For MySQL, we can set the launch as a Windows service and set a root password.
No additional configuration is required - just run the newly installed Visual Studio.
First project
Create a project in Ruby in Steel as usual in VS:
File - New - Project - Ruby In Steel (left) - Ruby Project (right) - Name (set the project name) - OK .
It is worth paying attention to the hierarchy of the organization of work with the code - the project (Project) contains one or more files in one or several folders, at the same time, the Solution (solution) may contain several projects.
After creating the project, it already contains one empty rubyfile.rb file, new files are added via the context menu of the project. Double clicking on the file name opens it in the editor. Further code will be written directly in rubyfile.rb - this is our test file.
First program
Traditionally, this is Hello World:
puts "Hello World!"
Oops, this is all ... Ctrl + F5 in the environment - and the result, so to speak, is obvious!
Games with strings
puts "Hello World!\nPrivet Mir!"
As you noticed right in the lines control sequences (Escape Sequences) work, incl.
\ n (new line),
\ t (tabulation),
\ s (space), etc.
puts 'Hello World!\nPrivet Mir!'
But the line enclosed in the apostrophes "stupid" - it does not understand
ES , so in most cases we use quotes. However, if it is known in advance that
ES in a line is not needed, then apostrophes allow to win in application performance.
puts 'It\'s Ruby'
Although no, the “stupid” line understands
\ ' and nothing else.
print "Hello World!"
print "Hello World!"
Unlike the puts operator,
print does not wrap a line after the output of a variable.
puts "2 x 2 = #{2*2}"
A remarkable feature of Ruby is a regular expression in a variable - you can insert pieces of Ruby code directly into strings using the
# {code} construct.
puts "#{"Ruby! "*4}"
“Multiply” the line by four and get the lyrics of the song Kaiser Chiefs;) And we do it all in one line of code!
puts "\n\t#{(1 + 2) * 3}\nGoodbye"
Try to imagine what this code will display and check your guesswork :)
Epilogue
For two "classes" we learned what Ruby and Rails are, learned their advantages, set up a training place, installed an IDE, wrote Hello World and even got a little acquainted with working with string variables in Ruby, doing without OOP.
I think that we will continue to learn the native, “clean” Ruby, leaving Rails in the storeroom for the future. I apologize in advance for this to those who hoped to immediately stamp sites. Further we will get acquainted with numerical variables, methods, local and global variables. Now there will be less text, more code.
PS: The well-known free benefits HUMBLE LITTLE RUBY BOOK by Jeremy McAnally and THE BOOK OF RUBY by Huw Collingbourne, special thanks to Google and Yandex, were used to write the record. As always, probably, there are errors in the text, inaccuracies - report them in the comments. If you know any original Ruby tutorials (available in English) that are available for download, I’m also waiting for you in the comments.
The third drop is here