Prateek vyas

In this post we will understand and see the code for bubble sort algorithm in ruby.

Bubble Sort Implementation in Ruby


  1. In bubble sort approach you’ll compare two adjacent elements and swap them if the succeeding element is smaller than the preceding one.
  2. You have to iterate through the array it’s length times.
  3. In every iteration that last element will be sorted.

Check out this code and run it

arr = [20, 2, 4, 21, 23,1, 2 ,3 ,4,17]arr.length.times do
arr.each_with_index do |e, i|
if arr[i] && arr[i+1]
if (arr[i] > arr[i+1])
arr[i], arr[i+1] = arr[i+1], arr[i]
p arr

Happy coding!




What would you do if you want to check if array consists of particular values?

This must be the first solution in your mind

[:sym1, :sym2].include?(:sym1)
#=> true

But what if i put all the elements as strings?

["str1", "str2"]

Now you have to check it with “str1" or “str2”

something like this

["str1", "str2"].include?("str1")

So you don't have a feasible solution!

That’s where ActiveSupport::ArrayInquirer comes into the action.

You just need to create the array object of ActiveSupport::ArrayInquirer

ar =["str", :sym, "str2"])ar.str? #=> true
ar.sym? #=> true #=> false

We also have any? method for this object, which doesn’t care about symbols or strings! and gives true if any of the values match!

ar.any?(:str) #=> true
ar.any?("sym") #=> true

Hope this blog post added a value in your knowledge about ActiveSupport

Happy coding!



Hi, This solution is implemented in Brute force algorithm.

Remove Nth Node From End of LinkedList

def remove_nth_from_end(head, n)
return nil if
length = 0
length_list = head
# Loop for calculating the length of linked list
while length_list
length += 1
length_list =
if n < length
l_dash = head
data = head
l_dash_dash = l_dash
(1..(length-n)).to_a.each do |i|
if i == length-n =
# return
else =
l_dash =
data =

In the above code i have calculated the length of the linked list first

and then i traversed till (Length-n)th node and pointed it’s next to the next of next node.

NOTE: The above solution is not the best solution.

Happy coding!



No more errors on add_column and remove_column


In Rails, when we have a table and we want to add or remove columns from the table, thenwhat do we do?

we write migrations using methods like add_column for adding column and remove_column for removing a column.

Therefore we often get the errors like column already exists or doesn’t exists!

Now we got 2 methods to ignore these errors.

  1. ) if_not_exists

you can pass if_not_exists: true. for the migrations of adding a column

for eg:-

class AddArticleIdToComment < ActiveRecord::Migration[6.1]
def change
add_column :comments, :article_id, :string, if_not_exists: true

Similarly we have if_exists

2.) if_exists

you can pass if_exists: true. If you’re removing a column

For eg: -

class RemoveArticleIdFromComment < ActiveRecord::Migration[6.1]
def change
remove_column :comments, :article_id, :string, if_exists: true


You just completed a post!

Happy coding!