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 toplevel 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 names 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, evaluating 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 result yielded by eval, and prints it out to the user. If it is a complex expression, it may be pretty-printed to make it easier to understand.
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 need to enter some expressions inside the REPL
* REPL only lives in memory, so one you shut down the REPL, the data disappear
* REPL acts like an evaluator of the expressions given by the developer and shows a result or an error, so for that reason some developers call it: an interpreter
* It is possible to build a REPL for almost any programming language, and so for pretty much every popular language, there is one that exists. However not all languages are distributed originally with a REPL
* There are cloud REPLs and there are 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 clear, it's time to advance to a new concept: Text Editors in Coding
With REPLs as we’ve just learnt can help us to test Ruby code, but REPLs just live in memory and it just can be executed line by line, which means that you cannot create a bunch of files with Ruby code inside and then connect all of them. 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 can to execute 500 or 5.000 lines of code, all of them in one file or in separate files where they connect with each other, you’ll need a text editor to write, save in a hard disk and debug all of that code. After you have ready all of that code, you can then execute with a REPL.
Here is where things seems complicated but it's actually quite simple if we explained like a series of steps:
- * You have a console or terminal in your machine.
- * A console or terminal traditionally refers to a computer terminal where a developer may input commands and view output such as the results of inputted commands or status messages from the computer.
- * Inside the terminal you can use a REPL program like IRB to test quickly some Ruby code, you like it and then you decide to create a more robust program, like a web app, so you have to use a text editor to create, save or update the bunch of lines of code you want to build
- * A text editor is a software program where you can 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 and more complex like Visual Studio Code
- * Complex text editors like Visual Studio Code can highlight the sintax of the programming language, you can organize your project, you can find errors easily or you can even install 3th party packages or libraries who help you with different tasks while you’re coding. While editors like Word is meant to other purposes but it helps us to do this analogy (almost anybody uses Word to code)
- * Once you have your code ready in a text editor you 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
All of this means that we have 2 different ways to execute Ruby code
1- Execute Ruby code with IRB (just as we’ve learned in previous posts)
This is the screenshot of my terminal
This is the screenshot of my IRB in the terminal
2- Using a Text Editor
Here you need to create a folder, then a file with the .rb extension and then the code you want to execute
But as you can see, the output wasn’t executed (we don't have the output equal to 3), so we have to come back to our terminal, and inside the folder execute the code like so
$ ruby file_name.rb
Now we have the number 3 printed in a different way. However this looks unnecessary but it's not. 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 :posts
has_many :links
has_many :items, dependent: :destroy
## Set Role
enum role: [:user, :admin]
before_create :set_default_user
# after_create :send_welcome_email
# after_create :send_welcome_email_job
def set_default_user
self.role = :user
end
# Replaced by BackgroudJob
# def send_welcome_email
# UserNotifierMailer.welcome_user(self).deliver_now
# end
#def send_welcome_email_job
# WelcomeEmailJob.perform_later(self.id)
#end
end
What does it look like in IRB? Let’s copy and paste that code in 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 is fair enough to say that is more difficult to interpret in IRB in comparison with a Text Editor… let’s check in a text editor
Now we have more clarity about Ruby comments, classes, methods and variables. Can you see now the advantages of using a text editor? I hope so. It’s crucial to know how to deal with it, because we’ll spend a lot of time inside them. I hope you're now convinced about the necessity of a text editor
Now, what are the best text editors out there?
Actually there are a lot of text editors, you just need one. In my whole career I have used just 3 of them
Notepad++: it was the first editor I knew. At that time it was so popular, so it was my first editor
Atom: then Github launched Atom text editor, which is still a good option if you want to have just an Editor
Visual Studio Code: now I’m using an IDE, 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. However I think is still the best option now out there, they have a huge community, supported by Microsoft and with a lot of great plugins
Note: if you’re working with a virtual environment in AWS like we learned in previous posts, you DON’T need to install any text editor inside that machine. You have to download this software just to your development environment or local machine
It will look like something like this:
You have the left sidebar which is pretty useful
In that left sidebar you’ll have the explorer, search, secure control, run and debug and extensions. Lets go to extensions and right there you can find for Ruby, then Ruby Language and then install it
The extensions you can find in that search bar are one of the most powerful things in text editors, because with it you can debug, highlight the syntax of the language among other fancy things. I encourage you to make a Google search with the keywords: “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, for now I think we solved the question: What is a Text Editor for Coding?
Hope you enjoy it
Thanks for reading
DanielM