Thursday, February 24, 2011

Ruby array sorting

I recently had to come up with a way to sort an array of objects that looked something like this,
  ["a", "c", "b", "1", "10", "3", "thing4", 
   "thing3", "thing30", "thing2"]

We needed the text only strings first and the strings with numbers in them sorted by their numbers. The example arrays explain it better. Here's what I came up with:


This will sort that array so it comes out looking like this:
  ["a", "b", "c", "1", "thing2", "3", "thing3", 
   "thing4", "10", "thing30"]

Pretty, huh?

Wednesday, February 23, 2011

Inject

I just learned something awesome. I'm always doing something like this in Ruby,
  [4, 5, 6, 7].inject(0) {|t,v| t + v}

I could clean this up and do something much simpler using inject, like so,
  [4, 5, 6, 7].inject(:+)

What's more, you can run this too
  [4, 5, 6, 7].inject(:-)
  [4, 5, 6, 7].inject(:*)
  [4, 5, 6, 7].inject(:/)

And check this out, say I want the average of the numbers in this array. I just run this:
  [4, 5, 6, 7].instance_eval { inject(:+) / self.size.to_f }

Simply brilliant.

Monday, February 21, 2011

decent exposure

I was recently introduced to a nifty little gem called decent_exposure. This allows me to do something like the following in my controller

If this is my user controller, I can replace
  def index
    @users = User.all
  end
with
  expose(:users)

This definitely cleans up the code quite a bit. When things start getting hairy, you can redefine your expose method using a block, like so:
  expose(:users) do
    User.active
  end
This will grab all active users in your database (assuming you have a scope, in your model, that grabs all active users).
The gem does a lot more than I'm gonna cover here, I suggest taking a look at decent_exposure's github page for more info.

Wednesday, February 16, 2011

top!

I learned something really useful just now. I'm always opening up top and hitting 'c', 'M', every time. I did something few of us do today, though, I looked at the Man page :)

Do this
  - open up top
  - hit your favorite commands
  - now hit shift+w
This'll save your configuration info to ~/.toprc.

Now you don't have to hit a whole bunch of annoying letters every time you open up top. I did a bit of analysis on the amount of times I hit a command, out of the default whatever amount of saved commands, I hit top 166 times. hmm..
So I alias'd top to t, just 'cause I'm a bad mofo.

Friday, February 11, 2011

Makandra - Comparing two arrays

I recently came across a website called makandra.com. They have notes publicly available that they would normally only use internally. One not caught my eye, Check if two arrays contain the same elements with and without RSpec. The code for RSpec is straight-forward and simple but the code to duplicate that functionality is somewhat more than is needed. This function:

def difference_between_arrays(array1, array2)
  difference = array1.dup
  array2.each do |element|
    if index = difference.index(element)
      difference.delete_at(index)
    end
  end
  difference
end

could go away completely without any problem. Without this function, the second could be re-written like this and achieve the exact same functionality:

def same_elements?(array1, array2)
  extra_items = array1 - array2
  missing_items = array2 - array1
  extra_items.empty? & missing_items.empty?
end

I'm not sure if Henning Koch will come across this or not, but maybe it'll help him out (or anyone else that comes across this).