Skip to content
Determine steps to transform a list into another list
Ruby Shell
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.


Need to transform one list into another without wreaking too much havoc? Just follow the steps.

>> a x b)).transform(%w(a b c d e))
=> [
     [:delete, "x", 2],
     [:create, "d", 3],
     [:create, "e", 4],
     [:move, "a", 0],
     [:move, "b", 1],
     [:move, "c", 2]

Each step is [action, item, index] where action can be

  • delete
  • create
  • move


Add this line to your application's Gemfile:

gem 'transformable_list'

And then execute:

$ bundle

Or install it yourself as:

$ gem install transformable_list


You can change what items are considered equal by supplying a matcher. foo), matcher: -> a, b { a.size == b.size })

The default is -> a, b { a == b }.

Use case

A use case for this gem could be an ActiveRecord has_many association that maintains a sequential index field. Let's say you have a voting system, and a user would assign all the choices by assigning an array. When you are updating existing choices into new ones, you want to keep most of them intact. You can then use logic like this:

original_list =, matcher: -> a, b { a['text'] == b['text'] })
steps = original_list.transform(new_list)

steps.each do |step|
  action, item, index = step

  case action
  when :delete then item.destroy
  when :create then choices.create(text: item['text'], index: index)
  when :move then item.update_attribute(:index, index)

Algorithm efficiency

The gem was not intended for big lists, and efficiency wasn't a concern. If you have ideas on how to make it more efficient, PRs are very welcome.


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.