Ruby Programming for Beginners An Introduction to Learning Ruby Programming with Tutorials and Hands-On Examples (Metzler, Nathan) (z-library.sk, 1lib.sk, z-lib.sk)
Author: Metzler, Nathan
代码
No Description
📄 File Format:
PDF
💾 File Size:
4.3 MB
8
Views
0
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
Ruby Programming for Beginners An Introduction to Learning Ruby Programming with Tutorials and Hands-On Examples
📄 Page
4
Text Copyright © Lightbulb Publishing All rights reserved. No part of this guide may be reproduced in any form without permission in writing from the publisher except in the case of brief quotations embodied in critical articles or reviews. Legal & Disclaimer The information contained in this book and its contents is not designed to replace or take the place of any form of medical or professional advice; and is not meant to replace the need for independent medical, financial, legal or other professional advice or services, as may be required. The content and information in this book has been provided for educational and entertainment purposes only. The content and information contained in this book has been compiled from sources deemed reliable, and it is accurate to the best of the Author's knowledge, information, and belief. However, the Author cannot guarantee its accuracy and validity and cannot be held liable for any errors and/or omissions. Further, changes are periodically made to this book as and when needed. Where appropriate and/or necessary, you must consult a professional (including but not limited to your doctor, attorney, financial advisor or such other professional advisor) before using any of the suggested remedies, techniques, or information in this book. Upon using the contents and information contained in this book, you agree to hold harmless the Author from and against any damages, costs, and expenses, including any legal fees potentially resulting from the application of any of the information provided by this book. This disclaimer applies to any loss, damages or injury caused by the use and application, whether directly or indirectly, of any advice or information presented, whether for breach of contract, tort, negligence, personal injury, criminal intent, or under any other cause of action. You agree to accept all risks of using the information presented in this book. You agree that by continuing to read this book, where appropriate and/or necessary, you shall consult a professional (including but not limited to your
📄 Page
5
doctor, attorney, or financial advisor or such other advisor as needed) before using any of the suggested remedies, techniques, or information in this book.
📄 Page
6
1. Introduction Ruby is a cross-platform general purpose scripting language. Being a cross- platform language, it follows write once, run everywhere concept. When we say scripting language, in most cases, there is an interpreter which is responsible for executing the script. In this case, there is a Ruby interpreter which executes a Ruby script. Ruby supports multiple programming paradigms such as procedural, object-oriented and functional. This is a language that is easy to learn, read, understand and write. Hence it is often referred to as Programmer’s best friend. 1.1 History A Japanese computer scientist called Yukihiro Matsumoto started working on designing the Ruby language in 1993 and the first stable version of Ruby appeared in 1995. According to Matsumoto, Ruby is influenced by Perl, Smalltalk, Eiffel, Ada, Basic, and Lisp. The first version of Ruby was 0.95 released in 1995 followed by several stable versions such as Ruby 1.0 in 1996, Ruby 1.2 in 1998, Ruby 1.4 in 1999, Ruby 1.6 in 2000 and so on. At the time of writing this book (March 2020), the latest version is Ruby 2.7. 1.2 Supported Platforms Yukihiro Matsumoto first started developing Ruby on 4.3BSD , later moved to SunOS v4 and eventually to Linux . Today, Ruby supports most well- known platforms such as Linux, Windows, MAC OS, BSD (FreeBSD, DragonFly BSD, etc.), Solaris, AIX, Windows Phone, Windows CE, Symbian, etc . Ruby being a cross platform language, as long as there is no platform specific code in a program, a Ruby script written on one supported platform should work on another supported platform. For example, a script written on and for Linux should work on Windows without any problems. Ruby interpreters are also available for ARM-Linux platforms. Which means running scripts on single board computers (SBC) such as Raspberry Pi, Beagleboard, Asus Tinkerboard S, Orange Pi, etc. is also possible. In the era of Cloud Computing, Ruby remains popular because of its power and ease of deployment on Virtual Machines (eg. AWS), Containers (eg. Docker), etc. 1.3 Ruby Implementations
📄 Page
7
The implementation language of Ruby is C. However, no C programming knowledge is required to learn Ruby. The de-facto reference implementation of Ruby by Yukihiro Matsumoto himself is called Matz’s Ruby Interpreter, MRI or CRuby and is open-source. Alongside this, there are many more alternate implementations such as JRuby (Implemented in Java, runs inside JVM), IronRuby (Implemented in C#, runs inside .NET framework), Rubinius (Written in C++ and Ruby) and many more. In this book, we will stick to MRI.
📄 Page
8
2. Scope A wide variety of things can be done using Ruby. This is the beauty of being a general purpose programming language. It is possible to build desktop applications, web applications, web services, etc. with Ruby. In fact, Ruby is a very powerful language on the web. There is a widely used web framework called Ruby on Rails which powers thousands of well-known websites such as GitHub, Airbnb, ASKfm, fiverr, Kickstarter, SlideShare, etc . GUI applications for desktop can also be developed using Ruby with the help of appropriate bindings of GUI frameworks such as shoes, gtk2/3, etc . Most databases such as MySQL, MSSQL, SQLite, PostgreSQL, etc. are supported by Ruby. Ruby projects can be easily extended using packages called as gems . The official package manager is known as RubyGems which hosts thousands of gems. 2.1 Prerequisites Even if you have no programming background, you should have no problems in learning basic Ruby. However, you should be comfortable with using your computer and be well versed with using Shell/Terminal on Linux/MAC and Command Prompt/PowerShell on Windows. I am not saying that you should be a wizard of any sort but should know the basic commands, be comfortable with file system navigation using Shell, Terminal, Command Prompt or PowerShell. If you have any programming knowledge at all, you will really enjoy learning Ruby. If you have a choice to learn any other programming language before Ruby, I would suggest C or C++. What will I learn from this Ruby book? This book will teach you to write simple console applications in Ruby. When you come to the end of this book, you will be able to write Ruby scripts that interacts with the user, interacts with the file system, etc. Although Ruby is very useful on the web, this book does not contain any lessons on web specific development because the prerequisite of that is knowing basic web development using languages such as HTML, CSS, Javascript, etc. Having said that, if you are a web developer or know even the basics of web development using HTML, CSS, Javascript, etc., the basic knowledge of
📄 Page
9
Ruby that you gain from this book should be enough for you to self-learn web development using Ruby.
📄 Page
10
3. Getting Started You will need a PC/Laptop with Windows/Linux having a reasonably good hardware configuration or a MAC machine to write and execute Ruby scripts/programs. Ruby scripts can be written using any text editor including Notepad. I suggest Notepad++ (https://notepad-plus-plus.org/ ). Ruby scripts are plain-text files and carry the extension .rb . Ruby is an interpreted language and a Ruby interpreter is needed to execute Ruby scripts. We will now see how to get started with Ruby interpreter on different platforms. 3.1 Installing Ruby on Windows Ruby MRI is the reference implementation of Ruby and can be downloaded from https://www.ruby-lang.org/en/downloads/ or https://rubyinstaller.org/downloads/ . Download the installer which includes MSYS2-Devkit . This Devkit is required to compile Ruby Gems with C- extensions . This is an advanced topic and there are no examples in this book that deal with compilations of Gems with C-extensions. However, it is good to have the Devkit installed because should you choose to self-learn advance Ruby concepts, you will have all the tools ready on your system. To give you a context, I have chosen Ruby+Devkit 2.6.5-1 (x64) installation file. You can choose any version of Ruby, later the better. Once you have the appropriate installation file, execute it to begin the installation process. You will see something like this:
📄 Page
11
Read the agreement, accept the terms and click Next .
📄 Page
12
Here, you can choose the directory where the Ruby environment should be installed. It is best to leave it unchanged. Check the options as marked in the image above and hit Install .
📄 Page
13
Make sure both the items are checked and click Next .
📄 Page
14
The installation process will now begin and you will see something like this. The whole process may take a few minutes to complete. When it is done, you will see something like this:
📄 Page
15
At this point, you will have Ruby environment on your system. Installing MSYS2 is an optional step. You can either do it now or later. If you chose to install it now, check the Run ‘rdk install’ option and click Finish . A console application like the one shown in the image below will launch.
📄 Page
16
Type 1, press Enter and follow the instruction. Now that the installation process has finished, let us make sure that the Ruby environment has been set up and ready for use. To do so, open Command Prompt or PowerShell and enter the following command: ruby -v If you see this command return Ruby’s version as shown in the image above, it means Ruby has been installed, the environment has been correctly set up and you are good to go! If you see an error message which looks somewhat like this – 'ruby' is not recognized as an internal or external command, operable program or batch file. Or something similar, it means there is something wrong. In such a case, kindly go through the installation process all over again.
📄 Page
17
3.2 Installing Ruby on Unix based OS If you are using Unix based systems such as Linux, BSD (FreeBSD, DragonflyBSD, etc.), macOS, etc., it is most likely that your system already has Ruby environment installed. To check, open Terminal/Shell and enter the following command: $>ruby -v If this command returns the version of Ruby (just like on Windows) as shown in the screenshot below, it means Ruby environment is present and ready for use: Note: Ruby version on my machine is an older one but would still do the job. This image is for demonstration purpose only. If the Shell/Terminal says command not found or something similar, it means that the Ruby environment is not present and will have to be installed and/or configured. To do so, visit https://www.ruby-lang.org/en/downloads/ and download the appropriate version of Ruby for your operating system. 3.3 Writing Ruby Scripts A Ruby script is a collection of instructions inside a plain-text file that a Ruby interpreter can understand. As mentioned earlier, Ruby scripts or programs can be written using text editors such as Notepad, Wordpad, vi, emacs, etc. I will be using Notepad++ . A script once written should be saved with the extension .rb . This file is referred to as – Ruby program, Ruby script, Ruby source, source code, source file or simply script/program/source. On Windows, simply open your favourite text editor, write the script and save it as <file name>.rb . On Unix based systems such as Linux, MAC, FreeBSD, etc., you have to do one extra thing – add a shebang line . A
📄 Page
18
shebang line is a sequence of characters beginning with #! followed by the location of the environment or the script interpreter. In this case, it is the Ruby interpreter. This line should be the first one in your script. The Ruby interpreter is mostly located at /usr/bin/ruby . Hence, on Unix-like systems, the shebang line will look like: #!/usr/bin/ruby If you are unsure of the location, you can use where or locate commands to determine the exact location of the Ruby interpreter: $>where ruby OR $>locate ruby On MAC, you can use the which command: $>which ruby These commands will return full path to the Ruby interpreter, simply copy it and use it as a part of your shebang like. Note: A shebang line is not mandatory but is considered as a good programming practice. Hence, it is recommended that you insert this line in your Ruby scripts on Linux/MAC. 3.4 Executing Ruby Scripts Ruby scripts can be executed using the ruby command through Command Prompt/PowerShell on Windows and through Shell/Terminal on Linux, MAC and other Unix based systems. A general way to use this command for script execution is: ruby <script name> Example: ruby myscript.rb The <script name> mentioned in the above syntax is a command line argument that is passed to this command. Let us now get a hands on experience with Ruby script execution on Windows and Unix-like systems. 3.4.1 Ruby Script Execution on Windows When we say ruby command on Windows, we refer to the Ruby interpreter
📄 Page
19
ruby.exe which is located inside the Ruby environment’s installation directory. If you did not change the installation directory, it will be most likely C:\Ruby<version>\bin . In order to execute a Ruby script on Windows, open Command Prompt or PowerShell, navigate to the directory where the script is present and run the following command: ruby <script name> #OR ruby.exe <script name> Example: ruby myscript.rb #OR ruby.exe myscript.rb 3.4.2 Ruby Script Execution on Unix-like OS On Linux, MAC, FreeBSD and other Unix based operating systems, there are two methods of executing a Ruby script. The first one is fairly straight forward, simply run the ruby command and supply the script name as an argument. When we say ruby command on Unix based operating systems, we refer to the Ruby interpreter’s binary which is usually located at /usr/bin/ruby . In order to execute a Ruby script on Unix based systems, open Shell/Terminal, navigate to the directory where the script is present and run the following command: ruby <script name> #OR /usr/bin/ruby <script name> Example: ruby myscript.rb #OR /usr/bin/ruby myscript.rb There is another method where you make the Ruby script itself executable. To do so, the script in question must be given executable permissions. The best way to do it is use the +x option with the chmod command as follows:
📄 Page
20
chmod +x <script 1>, <script 2>, … <script n> Example: chmod +x myscript.rb Once execute permission is given, the script can be executed as follows: ./<script name> ./myscript.rb Note: This method requires you to have the correct shebang line. The shebang line specifies which interpreter/environment to use to execute the script. If you do not use a shebang line or use an incorrect path, the correct interpreter/environment will not be invoked and your script will fail to execute.
The above is a preview of the first 20 pages. Register to read the complete e-book.
Recommended for You
Loading recommended books...
Failed to load, please try again later