Functional Style Sorting in Ruby

Last week I was discussing with @romandreg about how sort algorithms could be so intuitive when you are thinking in a functional style. They become very succinct and expressive. After some thinking I implemented a version of insertion sort and quicksort in ruby following a functional style. First let’s see an example of insertion sort in a classic imperative style:
class Array
  def insertionsort!
    1.upto(length - 1) do |i|
      value = self[i]
      j = i - 1
      while j >= 0 and self[j] > value
        self[j+1] = self[j]
        j -= 1
      self[j+1] = value
n a functional style, you could think that there are two functions involved when you are doing insertion sort. One that inserts a given value where it belongs and another that reduces the original array using this function:
class Array
  def insertion_sort
    # Function to insert a value where it belongs
    # in an ordered array.
    insert_in_place = -> (array, new_elem) do
      head, *tail = array
      return [new_elem]  unless head
      if head > new_elem
        [new_elem] + array
        [head] +, new_elem)

    self.reduce([], &insert_in_place)
Lets see how this same of kind of thinking applies to quicksort:
def quicksort(array)
  pivot, *tail = array
  return [] unless pivot
  left = { |elem| elem < pivot }
  right = { |elem| elem > pivot }
  quicksort(left) + [pivot] + quicksort(right)
I don’t know about you, but I think this is super awesome.

You should also be careful to not have addresses which are valid
alternate syntaxes to the inet_ntoa() library call. For example 0xe
is a valid name, but if you were to type “telnet 0xe”, it would try
to connect to IP address It is also rumored that there
exists some broken inet_ntoa() routines that treat an address like
x400 as an IP address.

RFC 1912, Common DNS Operational and Configuration Errors

Rspec + Capybara + Poltergeist + TokenInput

Today I found myself writing an integration test where an input field was using token-input auto complete plugin. After a considerable amount of pain and without finding an out-of-the-box solution on google, here is a possible approach to solve the problem. It’s worth saying that is basically a merge between the approaches proposed here and here.

1) Add the following module in spec/support:

module TokenInputHelper
  def fill_token_input(locator, options)
    raise "Must pass a hash containing 'with'" unless options.is_a?(Hash) && options.has_key?(:with)
    page.execute_script %Q{$('#token-input-#{locator}').val('#{options[:with]}').keydown()}
    find(:xpath, "//div[@class='token-input-dropdown']/ul/li[contains(string(),'#{options[:with]}')]").click

2) Include the module in your spec helper:

RSpec.configure do |config|  
  config.include TokenInputHelper, :type => :feature

3) Finally, in your test you can do something like this:

fill_token_input 'advertiser_search_suggestion', with: 'Coca Cola'

This assumes that your view have an input with id “advertiser_search_suggestion”.

This got the job done for me. One thing that I would like to find a workaround is the “sleep”. If I don’t do that, capybara doesn’t wait for the AJAX response to come back and the test fails…