Prateek vyas

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

Bubble Sort Implementation in Ruby

Algorithm

  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]
end
end
end
end
p arr

Happy coding!

--

--

ActiveSupport::ArrayInquirer

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 string..like “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 = ActiveSupport::ArrayInquirer.new(["str", :sym, "str2"])ar.str? #=> true
ar.sym? #=> true
ar.abc? #=> 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 head.next.nil?
length = 0
length_list = head
# Loop for calculating the length of linked list
while length_list
length += 1
length_list = length_list.next
end
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
l_dash.next = data.next.next
# return
else
l_dash.next = data.next
l_dash = l_dash.next
data = data.next
end
end
l_dash_dash
else
head.next
end
end

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

add_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
end
end

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
end
end

Great!

You just completed a post!

Happy coding!

--

--