A little ruby array with symbols trick

In a Ruby on Rails application, when I would set up my routes I would always write:

resources :trip_gears, only: :create#if more than one CRUD method:resources :trip_gears, only: [:create, :index]

I just found out that you can do this differently, syntactically, by using %i if creating an array of symbols. It looks like this:

resources :trips, only: %i[index create]

This allows you to get rid of the symbols in the array as well as the comma. Just a different way to write it out but it does the same thing.

If you need to interpolate in the array. you would use a %I (uppercase). There are other ways to write percent string literals with ruby. Here is a list of some other ones:

--

--

Almost every Javascript interview will ask you what a closure is

For some reason, the definition of closure doesn’t seem to stick with me well. What is so closed about being able to access the variables inside your curly braces, the parent scope, as well as the global? I keep on having to look it up. But this is what I gather:

Closures are particular to Javascript. You will see them used more often with functions within functions.

function outer() {
const a = 23;
function inner() {
console.log(a);
}
inner();
}
outer();
//=> 23// The `inner` function is said to have closure over the variable `a`

It all has to do with scope and what is accessible within functions. Function inner() has access to constant a which is 23.

--

--