Stefan Wintermeyer PREFACE Don’t let people fool you into believing that Ruby on Rails is easy to learn. It is not! It’s probably the best and most effective framework to develop web applications but it is hard to understand in the beginning. The worst mistake of all is to not learn Ruby before diving into Ruby on Rails. To avoid it this book starts with the basics of Ruby. You will not become a Ruby guru after reading it but you’ll understand the basic ideas and that is important. I wrote this book for developers who learn best by following clean examples. I don’t like the idea of coding one big single application throughout a book but prefer smaller stand-alone code examples. Therefore you can skip a couple of pages or even complete chapters without losing context. Shameless plug: Please contact me by e-mail to [email protected] if you need Ruby on Rails consulting or training. Have fun with Ruby on Rails! Stefan Wintermeyer PS: I post updates about this book and Rails in general at https://twitter.com/wintermeyer Stefan Wintermeyer RUBY BASICS Introduction This book requires basic knowledge of HTML, plus the reader - you, in other words - should also have a basic understanding of programming. The beginning of this chapter is going to be a bit boring. Bear with me. It’s worth it. It is easy to program in Ruby, but Ruby is not a simple language.” ~ Yukihiro Matsumoto This chapter is a tightrope walk between oversimplification and a degree of detail that is unnecessary for a Rails newbie. After all, the objective is not becoming a Ruby guru, but understanding Ruby on Rails. I am going to elaborate on the most important points. The rest is then up to you. If you would like to know more about Ruby, then I recommend the book "The Ruby Programming Language" by David Flanagan and Yukihiro Matsumoto. The command ruby -v will print the current running Ruby version: $ ruby -v ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15] $ Hello World Ruby is a scripting language. So it is not compiled and then executed, but read by an interpreter and then processed line by line. A simple Ruby program hello-world.rb consist of the following line: Listing 1. hello-world.rb puts 'Hello World!' Use your favorite editor to open a new file with the filename hello-world.rb and insert the above line into it. You can then execute this Ruby program in the command line as follows: $ ruby hello-world.rb Hello World! $ A program line in a Ruby program does not have to end with a semicolon. The Ruby interpreter is even so intelligent that it recognizes if a program line was split over two or more lines for the sake of readability. I will spare you the corresponding examples and am only mentioning this so you don’t say or think later, "is it okay like this?" Indenting code is also not necessary. But it does make it much easier to read for human beings! puts and print If you go looking for examples on Ruby on the Internet, you will find two typical ways of printing text on the screen: ▪ puts prints a string, followed by a newline. ▪ print prints a string (without newline). Example program (an extension of the program hello-world.rb): Listing 2. hello-world.rb puts 'Hello World!' puts puts 'zzz' print 'Hello World!' print puts 'zzz' On the screen, you will see this: $ ruby hello-world.rb Hello World! zzz Hello World!zzz Comments A comment in a Ruby program starts with a #-sign and ends with a newline. As an example, I can add a comment to the hello-world.rb above: Listing 3. hello-world.rb # Program for displaying "Hello World!" # by Stefan Wintermeyer puts 'Hello World!' A comment can also follow a program line: Listing 4. hello-world.rb # Program for displaying "Hello World!" # by Stefan Wintermeyer puts 'Hello World!' # output A #-sign within strings in inverted commas is not treated as the start of a comment. Example program: Listing 5. hello-world.rb # Example program # by Stefan Wintermeyer puts 'Hello World!' puts '############' puts puts '1#2#3#4#5#6#' # Comment on this Help via ri When programming, you do not always have a Ruby handbook available. Fortunately, the Ruby developers thought of this and provided a built-in help feature in form of the program ri (of course only if you have installed the documentation which is the default). This is a typical chicken and egg situation. How can I explain the Ruby help feature, if we are only just getting started with Ruby? So I am going to jump ahead a little and show you how you can search for information on the class String: $ ri String [...] $