Category: programming

How to require multiple files in irb from command line

Recently, I had the need to run Ruby’s irb with multiple files already loaded.

After a bit of trial and error I came up with this which worked perfectly!

irb -r ./file1.rb -r ./dir/file2.rb

The files get loaded in order so if there are any dependencies in file2.rb on something in file1.rb, that works correctly.

Disable browser caching in Ruby Roda app

Recently, writing a plain Roda app, I was facing a caching problem. I had a list and a detail page and whenever there was a list item clicked on I was using the flash plugin to maintain the item selected and highlighting the item when the list page was loaded.

However, on navigating back via the browser button, the cached page was showing up and the highlight would be on the previous selected item or not there at all.

Continue reading

Windows – Path changes not updating

Multiple times when I change the path on Windows, via the ‘Environment Settings’ dialog, those changes don’t apply.

Even when I reopen the command prompt, the settings don’t seem to be there.

So, either I would log out of Windows completely and log back in or do a system restart. This was a big pain and God knows how much time I’ve wasted with this!

Recently, I got tired of this and decided to dig in a bit deeper and finally found the answer!

Continue reading

‘asdf’ not finding program

I discovered asdf quite late! I was relying on a bunch of different programs to manage Ruby (rvm), Node (nvm).

asdf was an amazing headache saver. It already has core plugins for Elixir, Erlang, Node and Ruby. Plus, it has community plugins for what seems like practically anything under the sun.

I had installed asdf on a new system. And as usual, there were some crons to run on it. I kept getting a /bin/bash/ruby not found error.

After some time I realized I had faced this problem before as well!

Continue reading

Ruby Sequel migration – increment column in a group

The requirement was to add a column called position to the order_details table.

The order_details table is actually a table with a foreign key of order_id. The requirement is to increment the position starting from 1 in the position column grouped on order_id.

So, before the migration if the records are –

id  order_id
1   1
2   1
3   1
4   2
5   2
6   2

After the migration, the records should be –

id  order_id position
1   1       1
2   1       2
3   1       3
4   2       1
5   2       2
6   2       3

There are two solutions possible. One using window functions and one without.

Continue reading

Installing nvm and node on Linux

I’m using PM2 for managing all the server processes. So, installing node and nvm (node version manager) is now one of the first things I do during an application server setup.

node and nvm, just like ruby and rvm (ruby version manager) are always installed under the non-root user account.

Continue reading

Promises and Async / Await

I always struggle with understanding how async / await works.

So, I decided to write the code down and refer to this rather than trying to think it though every time!

I’ll start with the simple XMLHttpRequest

var req = new XMLHttpRequest();

req.onreadystatechange = function () {

  req.onload = function () {
    if (req.status == 200) {
      console.info('success')
      var arr = JSON.parse(req.responseText);
      populatePage(arr);
    }
    else {
      console.info('error')
    }
  }
}

req.open('GET', baseURL, true);
req.send();

Here, I’m hitting baseURL (defined outside of this code) and doing something with the received array with the populatePage function outside as well.

I wrote it like this so that I don’t add other code which is not relevant to the call.

Continue reading

Backup / Restore Postgres, MySQL, SQLite database using Sequel

Sequel is a fantastic ORM available in Ruby.

It also comes with an IRB console so it can be run directly from the command line with various options.

For example, to console into an SQLite database testdb.sqlite in the current directory –

$> sequel sqlite://testdb.sqlite

Or for Postgres –

$> sequel postgres://dbuser:dbpassword@localhost/testdb

However, I use sequel most frequently to take quick database backups or to restore them.

Continue reading

Copyright © 2024 the möbius trip

Theme by Anders NorenUp ↑