Tag: ruby

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

‘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

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

error while running rake task – “Don’t know how to build task”

The default method to pass arguments to Rake tasks is to give the parameters in square brackets –

desc 'Method #1: Use the default rake way to add two numbers and log the result'
task :add, [:num1, :num] do |t, args|
  puts args[:num1].to_i + args[:num].to_i

Reference: 4 ways to pass arguments to a Rake task

$ rake add[1,2]
# => 3

However, I would rather pass the arguments like this –

$ rake add 1 2
# => 3
Continue reading

ruby – difference between system, exec and backticks

There are multiple ways to run system commands from Ruby.

They are system, using backticks (‵), using %x(), using exec, or the more advanced option of using Open3. All of them behave in different ways.


The system method calls a system program. You have to provide the command as a string argument to this method. For example:

>> system("date")
Sat  4/10/2021   9:11:38
=> true

Note: In Windows, the date command also takes the input to change the system date after displaying the current system date. Press Enter to continue

The invoked program will use the current STDIN, STDOUT and STDERR objects of your Ruby program. In fact, the actual return value is either true, false or nil. In the example the date was printed through the IO object of STDIN. The method will return true if the process exited with a zero status, false if the process exited with a non-zero status and nil if the execution failed.

Continue reading

ruby strftime options

Every time I need to format ruby’s strftime options, I need to search for it on the internet. Putting them down here so that I don’t have to do that again!

Date Formatting 1

%YYear with century (e.g. 2015, 1995, etc)
%mMonth of the year, zero-padded (01..12)
%BThe full month name (e.g. January)
%bThe abbreviated month name (e.g. Jan)
%dDay of the month, zero-padded (01..31)
%jDay of the year (001..366)


Don’t pad a numerical output
_Use spaced for padding
0Use zeros for padding
^Upcase the result string
#Change case
:Use colons for %z
Continue reading

Copyright © 2024 the möbius trip

Theme by Anders NorenUp ↑