Previously we've learned about REPLs
and what kind of task we can achieve with it. Let's re-check some basic concepts about it:
REPL is an interactive top-level or language shell. REPL is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise. In a REPL, the user enters one or more expressions (rather than an entire compilation unit), and the REPL evaluates them and displays the results. The name read–eval–print loop comes from the terms of the Lisp primitive functions which implement this functionality:
- * The read function accepts an expression from the user and parses it into a data structure in memory. For instance, the user may enter the s-expression (+ 1 2 3), which is parsed into a linked list containing four data elements.
- * The eval function takes this internal data structure and evaluates it. In Lisp, considering an s-expression beginning with the name of a function means calling that function on the arguments that make up the rest of the expression. So the function + is called on the arguments 1 2 3, yielding the result 6.
- * The print function takes the evaluation result and prints it out to the user. It may be pretty printed to make it easier to understand if it is a complex expression.
Please refer to this article for more details about REPL
I know this is a lot of new jargon, but in plain English, the key concepts we have to take away are:
- * User or developer is the one who needs to enter some expressions inside the REPL
- * REPL only lives in memory, so once you shut down the REPL, the data disappear
- * REPL is an evaluator of the expressions given by the developer and shows a result of an error, so for that reason, some developers call it: an interpreter
- * It is possible to build a REPL for almost any programming language (even you can create one if you want), and so for pretty much every popular language, there is one that exists (but you don't have to write one by yourself because of this). However, not all languages are distributed originally with a REPL (almost all modern programming languages have a REPL, so don't worry about it)
- * There are cloud REPLs and REPLs that come with your favorite language as a default, like in our case with Ruby (IRB).
Well, now that we have this concept more precise, it's time to advance to a new idea: Text Editors in Coding.
Text Editors in Coding
REPLs, as we've just learned, can help us test Ruby code, but REPLs just live in memory and can be executed only line by line, which means that you cannot create a bunch of files with Ruby code inside them and then connect all of it. It's for this reason that we need a text editor. If you have just 5 or 6 lines of code to execute and test, you can run a REPL like IRB, but if you want to complete 500 or 5.000 lines of code in separated and organized files and where they can connect with each other, you'll need a text editor to write, save (in a hard drive) and debug all of that code. Then, once you have read that code, you can execute it with a REPL.
Here is where things seem complicated, but it's actually quite simple if we explain all of these like a series of steps:
- * Step #1: You have installed a console or terminal in your machine (it comes by default with any Operating System), and you know the primary usage of it. A console or terminal traditionally refers to a computer terminal where a developer may input commands and view output such as inputted commands or status messages from the computer.
- * Step #2: Inside the terminal, you'll use a REPL program like IRB to test some Ruby code quickly, then you like the way ruby works, and finally you decide to create a more robust program, like a web app, so you would need to use a text editor to create, save or update the bunch of lines of code you want to build
- * Step #3: You have downloaded a text editor. A text editor is a software program to create, read, update, delete, save or debug your code. So yes, you'll need to install another tool in your machine called Text Editor. A text editor can be as simple as a Word document or a more complex one like Visual Studio Code. Complex text editors like Visual Studio Code can highlight the syntax of the programming language you choose, you can organize your project, you can find errors easily, or you can even install 3rd party packages or libraries that can help you with different tasks while you're coding. It's important to mention here that editors like Word have been built for other purposes in mind, but it helps us to make this analogy (because almost nobody uses Word to write code on it)
- * Step #4: Once you have your code ready in the text editor you choose, you'll have to execute that code in some way, so here you have to go again to your terminal, access to the folder where you have your newly created program and then execute that code and get a tangible output or result
Note: we'll be following all of these steps with a practical exercise next
Now, what are the best text editors out there?
Actually, there are a lot of text editors; you just need one. However, I have used just 3 of them in my whole career.
it was the first editor I knew. At that time, it was so popular, so it was my first editor.
then Github launched Atom text editor, which is still a good option if you want to have just an Editor (which is quite different from an IDE - later in another post, we'll be explaining these differences)
Visual Studio Code:
now I'm using an IDE (later we'll talk about the differences between text editors and IDEs), which is an evolution of the text editors, simply because they also have a terminal or console integrated. However, I don't like to use the terminal inside the text editor; I use them separately. But I think it is still the best option out there for now. They also have a vast community, supported by Microsoft and with a lot of great plugins (plugins is another name for 3rd party Packages or Libraries)
So let's focus on this last option (Visual Studio Code). First, you have to go to the main web page: https://code.visualstudio.com/
and then simply download it to your local machine.
Note: if you're working with a virtual environment in AWS, as we learned in previous posts, you DO need to install the text editor inside that machine.
It will look like this:
You now have this left sidebar which is pretty useful.
You'll have the explorer, search, secure control, run and debug, and extensions in that left sidebar. Let's go to extensions, and right there, you can find the Ruby extension, then Ruby Language, and then the only thing you have to do is install it.
The extensions that you can find in that search bar are one of the most potent things in text editors because, with them, you can highlight the syntax of the language among other bunch and fancy things.
Now, let's follow (with a practical exercise) the steps mentioned above.
Step #1- Using the Terminal
This is the screenshot of my terminal.
Step #2- Execute Ruby code with IRB (as we’ve learned in previous posts)
Step #3- Using a Text Editor
Here you need to create a folder in any place of your local machine, then a file with the .rb extension, and then the code you want to execute
Step #4- Executing the code that has been created inside the text editor
But as you can see, the output wasn't executed (we don't have the result equal to 3), so we have to come back to our terminal, and inside the folder, run the code like so
$ ruby file_name.rb
Now we have the number 3 printed differently. However, this looks unnecessary, but it's not. So let's check another example. What happens if we want to execute more code, something like this in our IRB interactive console?
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :items, dependent: :destroy
## Set Role
enum role: [:user, :admin]
# after_create :send_welcome_email
# after_create :send_welcome_email_job
self.role = :user
# Replaced by BackgroudJob
# def send_welcome_email
What does it look like in IRB? Let's copy and paste that code into IRB interactive console.
Probably a bit more complicated, with 31 lines of code, with some comments, without changing the color for Ruby classes, methods, and variables, so it is fair enough to say that is more difficult to interpret in IRB in comparison with a Text Editor… let's check this very same code in a text editor
With the text editor, we have more clarity about Ruby comments, classes, methods, and variables (because they have different colors and are highlighted). Can you now see the advantages of using a text editor? I hope so. It's crucial to know how to deal with it because we'll spend much time inside them.
I also encourage you to make a Google search with keywords like: "best visual studio code extensions," and then you could find so many cool things to do according to your own necessities.
Also, you can look for "best visual studio code extensions ruby," and with that, you can have the best extension in Ruby programming. Later in another post, we can talk about the best of it, but for now, I think we solved the question: What is a Text Editor for Coding?
Hope you enjoy it
Thanks for reading