rubyisforfun/manuscript/017.txt

78 lines
5.5 KiB
Text
Raw Normal View History

2018-12-26 03:28:44 +01:00
## Your First Program
In fact, your first program was `1+1`. But this time let's create a file with the name `app.rb` in your code editor with the following content:
{title="Your first program", lang=ruby, line-numbers=off}
```ruby
puts "I would hug you, but Im just a text"
```
Save the file and run Ruby interpreter with parameter `app.rb`:
```
$ ruby app.rb
I would hug you, but Im just a text
```
You can also type `ruby app.rb` in your file manager, but when you press Enter, result will disappear! The subtlety is that program runs, "works off", and quits. Control gets returned back to terminal or file manager. That's why we do not see result on the screen when you run a program from file manager. But we can toggle the output screen (hide panels) by clicking Ctrl+O.
Congratulations! You've written something meaningful and saved your program to disk. Let's improve it a little bit:
{title="Print a text and wait for Enter key", lang=ruby, line-numbers=off}
```ruby
puts "I would hug you, but Im just a text"
gets
```
In the program above we put a string on the screen and awaiting for the user input. Not just for any input, but for the whole string. Instruction `gets` stands for "get string", and string is not a character, it's a sequence of characters. That's why we can type any letters and must press Enter. Or we can press Enter without typing any letters, in this case `gets` will _return_ empty string (we'll discuss _return values_ later).
2018-12-26 16:58:00 +01:00
Run the program above and see what happens. If you run the program from file manager, result won't "disappear", because Ruby will be waiting for your input.
2019-01-05 06:36:19 +01:00
Now let's write super simple program to learn foreign language. Let's take three random Spanish words: la pelota (ball), la puerta (door), la paz (peace). Imagine that you need to learn these words. How would you do that using a computer? One of the ways is to emulate a teacher. Computer will ask questions, and we'll be just spelling translation out loud:
2018-12-26 16:58:00 +01:00
{title="Foreign language teacher program", lang=ruby, line-numbers=on}
```ruby
puts "How to translate la pelota?"
gets
puts "How to translate la puerta?"
gets
puts "How to translate la paz?"
gets
```
Try to run this program and... it works! Not very convenient, but it is something. There are no any answers, but we have questions. And result is great, only by using `puts` and `gets` we've just built something useful! If you're playing guitar, the following program is demonstration of how you can use your Ruby knowledge to teach yourself notes on a first string on fretboard:
{title="Guitar teacher program", lang=ruby, line-numbers=on}
```ruby
puts "Say a note on a 0 fret?" # The right answer is E
gets
puts "Say a note on a 1st fret?" # The right answer is F
gets
puts "Say a note on a 2nd fret?" # The right answer is F#
gets
puts "Say a note on a 3rd fret?" # G
gets
# ...
```
And so on up to 12th fret (E F F# G G# A A# B C C# D D# E).
Few notes about the listing above. As you may have already noticed, there are comments on some lines. Comments start with pound sign `#` (or "hash"). You can leave as many comments as you won't, including comments on the new line. Comments will not affect behavior of your program.
X> ## Exercise
X> Finish the program above if you understand music theory. If you don't, create a program to learn 10 foreign words. Try to add some comments to your program so you can understand it better.
2019-01-05 06:36:19 +01:00
Another note is about encoding. Since our program has only characters from A to Z, there is no need to specify any encoding. However, if you want to use Chinese, Russian, etc. characters, you have two options:
2018-12-26 16:58:00 +01:00
2018-12-30 08:52:53 +01:00
* If you're on Windows, first line of your program should be `# encoding: cp866` ("cp866" is the name of encoding). Also, the file should be saved using this encoding.
2018-12-26 16:58:00 +01:00
* You don't need to do anything if you're on MacOS or Linux
2018-12-30 08:52:53 +01:00
In other words, you may face encoding problems on Windows. There will be other minor issues over the course of this book if you're using Windows. Also, there is no any chance to be solid Ruby programmer on Windows (if you're not using workarounds like virtual machines).
2018-12-26 16:58:00 +01:00
2018-12-30 08:52:53 +01:00
It doesn't mean that Windows operating system sucks, not at all. The only reason is that some gem authors (gem is a small Ruby module, to be covered later in this book) do not test their code on Windows. Don't blame them, because historically Ruby community and infrastructure evolved around free and open source tools, and Linux was the number one platform for Ruby. MacOS was the second, because it supports POSIX (Portable Operating System Interface), and all existing tools were somewhat compatible. Nobody was thinking about Windows, and to be honest Microsoft didn't pay too much attention to open source when it all started.
So we highly recommend switching to free operating system like Linux Mint Cinnamon or MacOS.
X> ## Exercise
X> If you're running Windows, _make an attempt_ to download and install [VirtualBox](https://www.virtualbox.org/) (free) or VMWare Workstation (requires a license). With this software you'll be able to run Linux inside of your Windows box. You will need to install [Linux Mint Cinnamon 64-bit](https://linuxmint.com/download.php). Try to open up the terminal, install Midnight Commander, and write your first program in Linux. Do not expect immediate success, it might take a couple of tries the first time. If it doesn't work, feel free to come back to this topic later. Important step here is to identify if your computer hardware supports virtualization. If not, you'll probably need to install Linux _instead_ of Windows (recommended) or upgrade your computer.