How to fix: brew update files overwritten by merge

Sometimes when you run:

brew update

You get an error:

brew update the following untracked working tree files would be overwritten by merge

According to the repo wiki for Homebrew this is caused by an old bug in update that has long since been fixed.

Basically you have changed (uncommitted) files in your Homebrew working directory that you need to deal with. You simply need to reset the working directory so your update command can pull the latest files. If you’re familiar with git – then this will all make sense, if not, just run these 3 commands and your Homebrew will be fixed!

cd /usr/local
git fetch origin
git reset --hard origin/master

How to install and use Homebrew

If you’re doing any Ruby on Rails or other open source development on your Mac, you’re going to need Homebrew.

What is Homebrew?

Homebrew is a package management system for your Mac. Homebrew makes it easy to install the software you generally need for Ruby on Rails development. Sometimes when you install RubyGems for your project, they will be dependent on software being installed on your Mac. One of the more obvious examples would be a MySQL gem and the actual MySQL Database Server. You install the MySQL Database Server using Homebrew! One of the less obvious examples would be the Nokogiri gem. This gem uses a software package named libxml2. You would install the libxml2 software package using Homebrew in order to install the Nokogiri RubyGem.

So how do you install Homebrew?

Installing Homebrew is dead simple. However, it does have a rather large dependency. You will need the command line tools addon of Xcode installed. Xcode is made by Apple and its used to write Mac and iOS applications. Xcode is free and all you need to do is search for it on the Mac App Store and click install. Its a rather large application, so it may take a while to download and install.

Once you’ve finished installing Xcode. You will need to open Xcode and choose Xcode -> Preferences. Choose the Downloads tab and install the Command Line Tools.

Xcode Command Line Tools

This will take some time, but when its done your Mac will be ready to install Homebrew!

If you visit the Homebrew website, you will see the following command:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

This command will need to be run in the OS X Terminal App. This means copy and pasting it into the Terminal window and pressing ENTER.

When the command is complete you should be able to run the following command in the Terminal window:

brew --version

If this works, then Homebrew is installed!

Now let’s go over a few common tasks you will have with Homebrew.

brew doctor

You want to use this command after system changes (such as OS X or Xcode updates) or if you think you need to troubleshoot your Homebrew installation. The output will tell you what’s wrong and generally how to fix it.

brew update

This will update your Homebrew formula list. Note, this doesn’t update all your installed packages! What this does is update the versions and availability of packages you could install. You want to run this often to keep your Homebrew installation up to date with the latest available software.

brew upgrade

This will upgrade your installed packages.

brew install [insert package name here]

This will install a software package onto your system.

brew list

This will list all the packages that are currently installed on your system.

brew info [insert package name here]

Sometimes after you install a package, you get a screenful of information or configuration steps. If you’re like me, you will probably blow right past that and keep working – then it will dawn on you that you probably should have read some of that stuff. This will let you go back and do that.

brew uninstall [insert package name here]

And finally, you may want to remove a package you previously installed. That’s what this is for.

So that’s a quick intro to Homebrew and how you can manage software dependencies for RubyGems on your Mac!

[FATAL] failed to allocate memory

I got this error today on my iMac. It was incredibly frustrating and I tried all of the following to fix it (the reason I list them is because I’m not entirely sure the final step I’ll mention at the end was the sole solution).

1. uninstall mysql2 gem (gem uninstall mysql2)
2. uninstall mysql on homebrew (brew uninstall mysql)
3. uninstall mysql-connector-c on homebrew (brew uninstall mysql-connector-c)
4. reinstall mysql on homebrew (brew install mysql)
5. reinstall mysql-connector-c on homebrew (brew install mysql-connector-c)
6. reinstall mysql2 gem (gem install mysql2)

After all that I still got the error… at this point I was about to set my hair on fire, but then I noticed I had receieved a difference error message in my logs…

ActionView::Template::Error (dyld: Library not loaded: /opt/local/lib/libssl.1.0.0.dylib
Referenced from: /usr/local/bin/node
Reason: image not found

Do you see that reference to node? WTF!

So then I did the following:

1. uninstall node on homebrew (brew uninstall node)
2. reinstall node on homebrew (brew install node)

WOOHOO! It worked again! My hair is safe and I can stand down from Defcon 1. Why did this happen? I have no idea. Hopefully this helps someone else and please let me know in the comments if you know why this happened.

XPath Query Returns No Results Using Libxml2 and GDataXmlDocument nodesForXPath

If you are using TouchXML or GDataXML for reading XML documents, you may have run into a problem with your XPath queries. I was working with the Freshbooks API to return a list of projects and it returned some fairly simple XML (I have abbreviated all the attributes of a project element for clarity):

<?xml version="1.0" encoding="utf-8"?>
<response xmlns="http://www.freshbooks.com/api/" status="ok">
  <projects page="1" per_page="15" pages="1" total="5">
    <project>
      <name>Super Fun Project</name>
    </project>
  </projects>
</response>

So now I wanted to simply retrieve all the project elements in the response using XPath. Seemed simple enough, so this is what I wrote:

// doc is an instance of GDataXMLDocument
NSArray *projects = [doc nodesForXPath:@"//projects/project" error:nil];
NSLog(@"%d", projects.count);

My NSLog statement always returned 0 results. This started to drive me nuts. I tried several different XPath queries and still no luck. There were no errors even when I used the error outlet, and I could even loop through the elements manually and see that there were indeed project elements being returned. So what’s the problem?

The problem had to do with namespaces. The XPath query needed to be written using the correct XML namespace, so something like this:

// doc is an instance of GDataXMLDocument
NSArray *projects = [doc nodesForXPath:@"//ns:projects/ns:project" error:nil];
NSLog(@"%d", projects.count);

Great, but what is the namespace name? According to my XML response it was http://www.freshbooks.com/api/. However the GDataXMLDocument knew nothing about it.

I started digging into the source of GDataXML and came across this in the nodesForXPath method:

int result = xmlXPathRegisterNs(xpathCtx, prefix, nsPtr->href);
if (result != 0) {
#if DEBUG
    NSCAssert1(result == 0, @"GDataXMLNode XPath namespace %@ issue",prefix);
#endif
}

Ok… some simple debugging showed it was actually registering a namespace, but what was it called? That lead me to some code just before the xmlRegisterNs call:

if (prefix == NULL) {
    prefix = (xmlChar*) kGDataXMLXPathDefaultNamespacePrefix;
}

Now we’re getting somewhere. Next stop the definition of the kGDataXMLXPathDefaultNamespacePrefix constant:

// when no namespace dictionary is supplied for XPath, the default namespace
// for the evaluated tree is registered with the prefix _def_ns
_EXTERN const char* kGDataXMLXPathDefaultNamespacePrefix _INITIALIZE_AS("_def_ns");

THAT WOULD HAVE BEEN HELPFUL AN HOUR AGO… Anyway, I went ahead and changed my original XPath query to:

// doc is an instance of GDataXMLDocument
NSArray *projects = [doc nodesForXPath:@"//_def_ns:projects/_def_ns:project" error:nil];
NSLog(@"%d", projects.count);

BOOM. Now we’re cooking with gas. It correctly returned all my project elements. So the lesson learned here is that GDataXML and TouchXML will not register your namespace or use it by default. You have to use the default, or register it manually.

How do you register a namespace with GDataXML manually you ask?

// doc is an instance of GDataXMLDocument
NSDictionary *ns = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.freshbooks.com/api/", @"fb", nil];
NSArray *projects = [doc nodesForXPath:@"//fb:projects/fb:project" namespaces:ns error:nil];
NSLog(@"%d", projects.count);

And finally, if you want to programatically register a namespace using GDataXml, then you would populate your NSDictionary by iterating through the namespaces found by GDataXMLElement:

// doc is an instance of GDataXMLDocument
NSArray *namespaceURIs = [doc.rootElement namespaces];

Hopefully that saves you some time and sanity.

using text_field_with_auto_complete and form_authenticity_token

Rails 2.0 introduced protection for Cross-site request forgery (CSRF). This is turned on by default in your Rails applications. However, many developers are turning off this valuable protection rather than making the changes necessary to use it. This seems the same to me as leaving your door unlocked while your friend visits because cutting keys is hard. Case in point: text_field_with_auto_complete

You know you have run into the new CSRF protection when you see this error in your Rails application:

ActionController::InvalidAuthenticityToken

This is the point where you Google the error and receive one of the following pieces of advice:

1. Turn off RequestForgeryProtection all together

# add the following line to your application.rb or one of your
# environment.rb files. why not production? (sarcasm)
config.action_controller.allow_forgery_protection = false

2. Turn off RequestForgeryProtection for just one controller

skip_before_filter :verify_authenticity_token

3. Turn off RequestForgeryProtection for just one action.

protect_from_forgery :except =&gt; :attack_me

So let’s see how easy it is to utilize this new feature!

I was working with the text_field_with_auto_complete in a pretty basic form:

For those of you who aren’t familiar with this there is a nice post on the Rails Wiki

When I tried to perform my auto complete I received the ActionController::InvalidAuthenticityToken error.

Why? Because basically my Ajax request did not include a security token that RequestForgeryProtection uses to validate my request is coming from the right place. All you need to do is add this token to the parameters sent with your request. Rails makes this easy with the helper: form_authenticity_token. The token name RequestForgeryProtection will be looking for is: authenticity_token.

So let’s add this to our text_field_with_auto_complete:

Now when I performed my auto complete it worked flawlessly! Now to refactor that string… any suggestions?

Update August 25, 2008

Learned two things working with this…

1. It’s automatically doing a POST, so by changing the method to GET I can avoid having to deal with the authenticity_token.

2. And with that out of the way I don’t need to specify the parameters since by default it will send back the value of the text field.

That’s a lot cleaner!

Learning Ruby on Rails with Heroku Episode 1

Ever since I saw Heroku I wanted to use it for a screencast on learning Ruby on Rails. This weekend I finished the very first episode! This episode is fairly light on Ruby on Rails content because I was so focused on screencasting and learning all about video and audio production for the web.

So take a look and tell me what you think. I used Vimeo for hosting the video. I also produced the video in HD… however I didn’t realize that Vimeo only streams the HD content on their site, not the embedded video. So to view the best possible video you should view it on Vimeo. here is the link. You can make it full screen and enjoy the HD in all its glory.

Please feel free to leave some constructive criticism regarding the screencast. This way I can make some changes before I start pushing this out more regularly.

In this first episode of Learning Ruby on Rails with Heroku:

  1. Introduction
  2. Goal of this screencast series
  3. What is Heroku?
  4. Getting an account
  5. Creating your first Ruby on Rails application
  6. Basic navigation of Heroku


Learning Ruby on Rails with Heroku Episode 1 from Matthew Carriere on Vimeo.

Using select, reject, collect, inject and detect.

I spend a lot of time convincing my friends to switch to a Mac. Some of my friends are also software developers so naturally, just when they think the evangelism has come to an end, I convince them to get on the Rails. However, learning Rails usually means learning Ruby for the first time as well. In this post I am going to address one of the issues I see for newcomers to Ruby. Looping.

Looping in Ruby seems to be a process of evolution for newcomers to the language. Newcomers will always find their way to the for loop almost immediately and when confronted with iterating an array, the first choice will generally be a for..in:

a = [1,2,3,4]
for n in a
  puts n
end

This works, but its not very… Ruby. The next stage of evolution will be using an iterator for the first time. So the for loop gets dropped all together and each is used. The Rubyist is born at this point:

a.each do |n|
  puts n
end

What I see next is a lot of conditional logic being used inside the each block. The logic is generally introduced to perform the following operations:

  1. Building a list of items from an array.
  2. Total the items in an array.
  3. Find an item in the array.

So if this is you, then stop. Ruby has plenty more iterators where each came from. Which one you should be using depends on what operation you are trying to perform. So let’s take a look at our previous list and see if we can find a more Ruby way to get them done.

Building a list of items from the array using select

For this operation you should be using select. The way select works is simple, it basically iterates through all the elements in your array and performs your logic on each one. If the logic returns TRUE, then it adds the item to a new array which it returns when the iteration is complete. Here’s an example:

a = [1,2,3,4]
a.select {|n| n > 2}

This will return the last two elements in the array: 3 and 4. Why? Because 3 and 4 are both greater than 2, which was the logic we placed in the block. It’s worth noting that select has an evil step sister named reject. This will perform the opposite operation of select. Logic which returns FALSE adds the item to the array that is returned. Here’s the same examples as before except we will swap select, with reject:

a = [1,2,3,4]
a.reject {|n| n > 2}

In this example the return value is [1,2] because these elements return false when the condition is tested.

I also have to mention another close sibling to select and reject; collect, which returns an array of values that are the RESULT of logic in the block. Previously we returned the item based on the result of the CONDITION in the block. So perhaps we need square the values in our array:

a = [1,2,3,4]
a.collect {|n| n*n}

This returns a new array with each item in our array squared.

Finally, note that using select, reject, and collect returns an array. If you want to return something different, because you are concatenating or totaling values, then check out inject.

Total the items in an array using inject

When you think of accumulating, concatenating, or totaling values in an array, then think of inject. The main difference between select and inject is that inject gives you another variable for use in the block. This variable, referred to as the accumulator, is used to store the running total or concatenation of each iteration. The value added to the accumulator is the result of the logic you place in the block. At the end of each iteration, whatever that value is, can be added to the accumulator. For example, let’s sum all the numbers together in our array:

a = [1,2,3,4]
a.inject {|acc,n| acc + n}

This will return 10. The total value of all the elements in our array. The logic in our block is simple: add the current element to the accumulator. Remember, you must do something to the accumulator in each iteration. If we had simply placed n in the block the final value of the accumulator would have been 4. Why? Because its the last value in the array and since we did not add it to the accumulator explicitly the accumulator would be replaced in each iteration.

You can also use a parameter with the inject call to determine what the default value for the accumulator is:

a = [1,2,3,4]
a.inject(10) {|acc,n| acc + n}

In this example the result is 20 because we assigned the accumulator an initial value of 10.

If you need to return a string or an array from inject, then you will need to treat the accumulator variable that way. You can use the default value parameter of inject to do this:

a = [1,2,3,4]
a.inject([]) {|acc,n| acc << n+n}

In this example I add n to itself and then append it to the accumulator variable. I initialized the accumulator as an empty array using the default value parameter.

Find an item in the array using detect

Our last example operation was to find an element in the array. Let’s just put it out there and say that other iterators could be used to select the correct value from the array, but I am going to show you how to use detect to round out our exploration of these iterators.

So let’s find the value 3 in our array using detect:

a = [1,2,3,4]
a.detect {|n| n == 3}

This returns 3. The value we were looking for. If the value had not been found, then the iterator returns nil.

So if your head is spinning at this point as to which iterator to use for when, then remember this:

  1. Use select or reject if you need to select or reject items based on a condition.
  2. Use collect if you need to build an array of the results from logic in the block.
  3. Use inject if you need to accumulate, total, or concatenate array values together.
  4. Use detect if you need to find an item in an array.

By using these iterators you will be one step closer to mastering… Ruby-Fu.

Installing MySQL on Mac OS X Leopard using MacPorts

There comes a time in every Rails developers life that they need to work with MySQL. Often this means that your development comes to a screeching halt as you wade through the various options for getting MySQL installed on your Mac.

This guide won’t help you install it anywhere else… because I am a zealot and I don’t care how to install it anywhere else.

Install MySQL using macports, go get a coffee while this runs its course.

1
sudo port install mysql5 +server

Next up, create the initial databases used by MySQL.

1
sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql

Configure MySQL to start at system start up.

1
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

Start MySQL.

1
sudo /opt/local/bin/mysqld_safe5 &

You can always stop the service manually as well:

1
mysqladmin5 -u root -p shutdown

Confirm that that MySQL is running.

1
mysqladmin5 -u root -p ping

Just hit enter at the password prompt, as it is blank. We are about to change that.

Secure your server.

1
mysqladmin5 -u root password [your password goes here]

At this point you should have your MySQL server up and running. However, your Rails project may complain about not being able to find the socket file. You can find that using the ‘status’ command.

First log into MySQL.

1
mysql5 -u root -p

At the prompt, run the command.

1
status;

In the output you will see an entry listing the socket being used by MySQL. You can use this in the database.yml file.

And finally, I really don’t like typing that ’5′ after all the commands… I can’t explain it, it just upsets me. So I like to create a symbolic link for the mysql5 and mysqladmin5 commands.

1
2
sudo ln -s /opt/local/bin/mysql5 /opt/local/bin/mysql
sudo ln -s /opt/local/bin/mysqladmin5 /opt/local/bin/mysqladmin

That should get you coding again against a MySQL database!

Learn to use your new Mac like a pro

Have you just switched to a Mac? Or have you switched sometime ago and still have those “how do I do this on a mac?” moments… well then this is for you! Apple has recently added a “Find out how” section to its website with videos on common computing tasks that should help your transition from the dark side.

Find out how