Mungomash

Chad Dalton - hacker

Dedupe an Array in Ruby

| Comments

Deduping an array in Ruby using the uniq method is simple.

1
["a", "a", "b", "b", "c"].uniq   #=> ["a", "b", "c"]

Deduping an array of objects based on a specific attibute can be more complicated.

Rails includes a uniq_by method.

1
[1, 2, 3, 4].uniq_by { |i| i.odd? } # => [1, 2]

If you are working without Rails, the Hash works nicely since it allows only one instance of each key.

1
2
a = [1, 2, 3, 4]
Hash[*a.reverse.map{|i|[i.odd?,i]}.flatten].values # => [1, 2]

Add Method to Existing Class in Ruby

| Comments

Classes are not locked down in Ruby. If you wanted to add the firstName method to the String class, you could call that method on any string. For example …

1
"Chad Dalton".firstName

As with most Ruby, the code is simple and straight-forward. Here is code that allows you to write 2.weeks.ago …

Git Cheat Sheet

| Comments

Powered by Octopress

| Comments

This site is powered by Octopress. I use Github Pages rather than pay for hosting. Octopress provides a guide that describes the set up necessary to deploy to github.

Google Cache

| Comments

Ever do something really stupid while making changes to a website and wish that you could step back in time and take a look at it the way it appeared yesterday … or more importantly take a look at the HTML before you hosed it up?

Well, luckily Google keeps a copy of every page it crawls. To see a cached page of your web site use this url:

http://webcache.googleusercontent.com/search?q=cache:URL

For instance, the cached version of the home page of this web site could be retreived as such:

http://webcache.googleusercontent.com/search?q=cache:http://mungomash.com

But, don’t wait too long. Eventually, Google will crawl your new hosed up version :)

Hello Hurricane

| Comments

Everyone has a “Hello World” program. I am not everyone. I prefer “Hello Hurricane.”

“I’ve got doors and windows
Boarded up
All your dead end fury is
Not enough
You can’t silence my love
Yeah I said hello hurricane”