rubyisforfun/manuscript/017.txt

72 lines
4.2 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.
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 way is to emulate a teacher. Computer will ask questions, and we'll be just spelling translation out loud:
{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.
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:
* 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.
* You don't need to do anything if you're on MacOS or Linux
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). The reason is XXXXXXXX So we highly recommend switching to free operating system like Linux Mint Cinnamon. You don't need to do anything if you're on MacOS.