Do you see the error in this simple Ruby snippet?
class Person
attr_accessor :name
def assign_name
name = 'akshay'
end
def greet
puts 'my name is ' + name
end
end
Keep reading if you’re curious.
Do you see the error in this simple Ruby snippet?
class Person
attr_accessor :name
def assign_name
name = 'akshay'
end
def greet
puts 'my name is ' + name
end
end
Keep reading if you’re curious.
Even though we use RSpec at work, more often than not, it just gets in the way. I’ve been using it for six months for now and I still have to have the docs open in browser to check the correct syntax. The paradox of choice is real with RSpec.
Often it feels like the RSpec was so busy coming up with all these sugary DSLs, it didn’t stop to think if they are actually needed.
I love minitest. Minitest is simple. There’re a few ways to write tests, and they are so intuitive. I learned it last year, when I got into Ruby, and since then I haven’t really felt the need to dive into the docs that frequently. It just works.
That said, I do find myself doing the minitest setup for my side-projects and quick-off learning projects often. Maybe you’re the same. So here’s a quick guide to rapidly get up and running with minitest in less than 2 minutes.
If you’re working on a giant rails app that takes a long time to boot whenever you run the tests, I highly recommend giving spring-commands-rspec
gem a try.
Spring is a Rails application preloader. It keeps the application running in the background, so you don’t need to load it every time you run a test, rake task, or even a migration.
Our application used to take almost a minute to load the files before running the tests. With Spring, it takes less than 5 seconds to run. Faster tests are great for developer productivity. Earlier, I used to wait for changes to accumulate before running a test. Now I can run the tests frequently after making small changes.
90% speed boost? I’ll take it.
This is the second post in the series on building a web application without using Rails. In the last post, we set up a very basic project structure for our web application which returns a hard-coded string response directly from the Ruby code.
class App
def call(env)
headers = { 'Content-Type' => 'text/html' }
return [200, headers, ['favicon']] if env['PATH_INFO'] == '/favicon.ico'
response = ['<h1>Greetings from Rack!!</h1>']
[200, headers, response]
end
end
In this post, we will separate the view from the application logic, and serve static files using the Rack::Static
middleware. If you prefer to watch a video instead, scroll to the bottom of this post.
Did you know that Rails has a ~/.railsrc
file?
Similar to your ~/.bashrc
or ~/.zshrc
file, you can use this file to configure your Rails applications.
This is especially useful if you find yourself repeatedly typing --skip
or --no-skip
commands and installing specific gems every time you create a new Rails application.
Since I made a switch to Rails from .NET last year, I must have created at least 20-30 projects for either learning, experiments, or client work. I have a text file that documents all the gems I want to install for a fresh Rails app. Every time I create a new project, I go through the file to install all the gems I want for my project.
With the ~/.railsrc
file, I don’t need to do that. Rails will do it for me. Here’s how it works.
This is the first post in the series on building a database-backed web application without using Rails. In this post, I will setup the project and create a very simple Rack application running on Puma app server. I will also show how to use Rack middleware to reload the application whenever the source code changes.
I hope that in the process, we all will learn more about how web applications work and get a big-picture understanding of the web frameworks, especially Ruby on Rails. As a side-effect, we will learn a lot of meta-programming in Ruby.
As a Ruby programmer, you probably know the &:
shortcut to call a method on each item in an array. For example, this snippet will call the to_s
method on :foo
and :bar
.
[:foo, :bar].each(&:to_s)
The above code is similar to:
[:foo, :bar].each do |item|
item.to_s
end
However, instead of calling a method on the array item, you want to call a method and pass the array item to that method. Is there a one-liner for that?
[:foo, :bar].each do |item|
puts(item)
end
It turns out, Ruby has a corresponding &method
shortcut to accomplish precisely this.
Two things are going on here.
You know about the PATH
environment variable. Did you know about CDPATH
? If you’ve been typing long cd
commands like me, this post is for you.
I came across this gem this morning, while reading Daniel Barrett’s Efficient Linux at the Command Line. It instructs the cd
command to search for the directory you specify in locations other than your current directory.
A cd
search path works like your command search path $PATH
. However, instead of finding commands, it finds the subdirectories. You can configure it with the shell variable CDPATH
, and it has the same format as the PATH
variable: a list of directories separated by colons.
Now you might think it’s so easy to access them using the ENV
hash. Why do we need a different way?
Well, a very common problem when working on multiple Rails applications on the same machine during development is that these applications might have environment variables with the same name. A good example is the database connection string. Different applications will have a different value for the same environment variable. If you set the environment variable in your shell config file, how can you use different values?
The same problem will happen in a Continuous Integration (CI) server, where you might have multiple projects running.
So, don’t set the environment variables on the development machines or CI servers. Use the dotenv gem that lets you load variables from a .env
file into the ENV
hash when the Rails application loads.
Let’s see how to use this gem.
You’ve been around in the Rails world for a while. You know your way around rails. But you keep hearing this word ‘Rack’ and don’t really understand what it is or what it does for you. You try to read the documentation on the Rack Github repository or the Rails on Rack guides, but the only thing it does is add to the confusion.
Everyone keeps saying that it provides a minimal, modular, and adaptable interface for building web applications. But what the heck does that really mean?
If there’s a list of topics that confuses most new Rails developers, Rack is definitely up there at the top. When I started learning Ruby and Rails last year, Rack took a really long time to wrap my head around. If you are in the same boat, fear not.
In this article, I will try to explain pretty much everything that you need to know about Rack as a Ruby and Rails developer. It covers everything from the basics to more advanced stuff such as middleware and writing your custom middleware. It’s quite a long article, but if you stick till the end, you will know more about Rack than pretty much everyone else.