5 examples of Ruby’s best usage
Have you ever wondered what we can do with Ruby? Well, the sky is probably the limit, but we are happy to talk about some more or less known cases...
A few years ago, I was grilling with some of my friends, all Java developers. Not sure how it came up, but we started talking about our coding environments. I told them I’m doing all my development using Vim and terminals. It triggered some jokes and funny questions, like if I’m still using punch cards 🙂
Since then, I tried some “real” IDEs but always came back to Vim and terminals as it’s a very fast method, has all the tools I need and I simply like working in the text mode.
Here are some details about my workflow. It’s worth mentioning I work on Linux, but you can set it up on Mac or Windows with no problems.
I start my working day with opening a terminal, cd ~/Projects/XYZ, and running tmux, so let’s start there.
What is TMUX? Like the authors say:
TMUX is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.
I use it to switch between multiple things in one terminal. So, in one TMUX window, I have the Rails server running, in the second I have my Vim opened, and every time I need to do something in the terminal, like checking routes, running migrations or tests, I just open a new window with CTRL-A C
and do the thing.
I don’t use TMUX panes, only windows, and do a lot of switching (I have CTRL + double-A bind to switching between last windows).
Another very convenient feature of TMUX is that it allows me to find and copy text from a terminal without using a mouse.
It’s the main part of my workflow. I won’t be getting into details about what VIM is and how it’s different from other editors. There is a lot of great articles and videos about it. In a few words, Vim is a modal editor. It has two modes, insert mode and normal mode. Insert is for typing text into a document and normal is for performing different operations on the document, like moving the cursor, deleting fragments of text, changing it, and so on. It sounds strange but after some time it feels very natural and it’s very hard to go back to your regular editor.
What I’d like to show you is the way how I combine Vim with other tools to get all I need as a web developer.
I’m not a fan of heavy plugin Vim configs, so I’m using only a few. Here is the list:
NERDtree – it is a file explorer. It allows you to easily do some basic file system operations, like moving files, renaming, creating new files, and many others,
vim-endwise – I’m a Ruby developer, so I use it to add end
after if
, do
, def
, and several other keywords,
vim-rails – it’s a Rails power plugin. It knows the structure of a Rails app. Want to check the partial under your cursor? Just press gf
and it opens it in your current window. It has many other options but that’s the one I use most often,
vim-surround – great plugin which makes working with surroundings very easy. With it, you can change the surroundings of the text from () to [] in a few keystrokes,
vim-repeat – complementary to Vim-surround. It allows for a repeating surrounding change with .
(repeat last change in Vim),
vimwiki– my way to take notes,
fzf.vim+ fzf – the core part of my web development workflow.
These are the tools that define the way I do my coding. I realized that usually when I’m doing some coding tasks, I spend most of my time on finding how the stuff works. To do this, I need to quickly check different parts of the codebase, methods definitions, classes, and other things.
Fzf is an interactive Unix filter for command-line that can be used with any lists (files, command history, git commits, process lists); in other words, it’s a fuzzy finder.
Ripgrep is an alternative to grep, but it’s the blazing fast one.
Once combined, these two let me quickly find anything I want in my project.
I have two settings in my Vim config file to help me with this:
nnoremap <C-p> :Files<Cr>
nnoremap <C-f> :Rg<Cr>
The first one let me quickly find correct files using fuzzy finding on file paths (CTRL-P). The second one runs the fuzzy finder on all paths and contents of files in my project using Ripgrep (CTRL-F).
So, let’s say I want to find migration where I added a column to the users
table. I hit CTRL-F and write something like adcolusnam
and it will match add_column :users, :name, :string
.
When I look for a method definition, I often run CTRL-F def method_name
.
It’s very useful as it searches through file path + content. It’s especially convenient when I know that the content I’m looking for is in a specific part of the project. For example, I want to find where we updated a username, and I remember it was somewhere in the controllers. I hit CTRL-F
conupdname
to match line app/controllers/users_controller: @user.update(name: new_name)
.
If you know your project and codebase structure, you can easily construct fuzzy find queries to quickly find almost anything you want.
Click here to see a short recording of me playing around with the `discourse` codebase (440k LOC on my i5, 16GB desktop) and showing how I usually move around the project.
I hope you got interested in my setup and decided to give Fzf a try in your coding endeavors. It really changed the way I’m using Vim as a code editor.