Xcode path

G’day guys,

Hope no one else has this issue! I ran into an error installing nodejs via brew because my xcode setup was corrupted. I believe the problem was caused by me installing xcode commnand line tools and GCC from github.

The error I got was Error: Can't run /usr/bin/usr/bin/xcodebuild (no such file). when running brew install node --verbose.

I ran xcode-select --print-path to make sure it resolved to /usr/bin, which it was. So I then had a look at /usr/bin/xcodebuild and to my surprise it was not a binary file but a shell script! Which was appeneding usr/bin onto the path retrieved from --print-path!

The fix was to grab a copy of the binary from another machine (shit I know!) and stuff it back into /usr/bin overriding the bash script which I assume came from the GCC installer.

Note For the boxen users I was also getting this error nodenv: couldn't find any version specified for use when trying to interact with node on the command line. Not sure why the boxen node install didn’t blow up

-Matt

Adding git pairs with Puppet or Boxen

Spent a little time looking for the best way to add Authors to the gitconfig using Boxen/Puppet and came up with this.

class config::git {

  package { ["edgecase-git-pair"]:
    ensure => installed,
    provider => "gem",
  }

  git::config::global{ 'color.ui':
    value => 'auto',
  }

  git::config::global{ 'push.default':
    value => 'simple',
  }

  git::config::global{ 'core.editor':
    value => 'subl -w',
  }

  define add_git_pair ($user_details = $title) {
    exec {"Add ${user_details} git pair":
      command => "git config --global --add git-pair.authors \"${user_details}\"",
      unless => "grep -c \"${user_details}\" /Users/${::luser}/.gitconfig"
    }
  }

  add_git_pair{"User Name1 <username1@email.com.au>":}
  add_git_pair{"User Name2 <username2@email.com.au>":}

}

Hope you found it useful :)

Using sudo on a remote sync session

Hey guys,

Quick one that helps when wanting to copy files over owned by root via rsync.

On the target machine

sudo vi /etc/sudoers

Append the following line the bottom of the file. Replace ‘dev’ with the user.

dev ALL= NOPASSWD:/usr/bin/rsync

Run rsync with --rsync-path

rsync -av -e "ssh" --rsync-path="sudo rsync" /source/ dev@target:/foo/

Pro Tip Monitor traffic with nettop -m tcp and look at the SSH processes

-Matt

Modifying Mac plist files

G’day Guys,

Playing around with Boxen and setting up some dock preferences. I wanted to be able to read and modify the preferences via the command line. Wasn’t straight forward so I thought I would share it.

Thankfully Mac OSX has something built-in to view and edit the plist files. It’s called PlistBuddy. (Tested with Mountain Lion).

It works by opening a plist file and then executing commands against it.

# Open plist
/usr/libexec/PlistBuddy ~/Library/Preferences/com.apple.dock.plist

# Print all file contents as JSON
command: print

# Show value
command: print autohide

# Update value
command: set autohide true

# Save and quit
command: save
command: quit

Note: Dock prefs only take effect when you restart the dock /usr/bin/killall -HUP Dock

-Matt

Capybara find error

Simple one I ran into when using Capybara.find(css_selector)

The following code

find(".manifest_section[data-title='#{parent_section_name}'] .new_section ")

Throws this error

Nokogiri::CSS::SyntaxError Exception: unexpected '$' after 'DESCENDANT_SELECTOR'

Remove the whitespace at the end yo :P

-Matt

Upgrading to Capybara 2

Hey guys,

Recently upgraded a Rails 3 project at work to Capybara 2 from 1.1.2. Ran into a few minor bumps along the way and which I’ve shared below and hopefully it helps others choosing to upgrade.

Useful links:

Most of this work was backwards compatible. I was able to push multiple fixes, keeping close to master and leaving the gem upgrade as the last commit.

The 4 biggest changes for us

wait_until

We have a large collection of features and a lot of those were using wait_until. Updating all our features to not use it was a large task so to keep moving I decided write a monkey patch which gives us access to a wait_until block. The idea being that we can than weed our features off the old behaviour one at a time instead of a big bang approach.

class Capybara::Session

  def wait_until(timeout = Capybara.default_wait_time)
    Timeout.timeout(timeout) do
      sleep(0.1) until value = yield
      value
    end
  end

end

find vs first

There were a lot of places in the code where we would use find instead of first. Most of these were easy to change but sometimes we needed to wait for the page and first wouldn’t cut it. To get around this I created a find_first method which would try to find it and if there were more than one just return the first (exactly what the old find did). This was another patch which was fixed up later with better use of selectors.

def find_first(locator)
  element = nil
  begin
    element = find(locator)
  rescue Capybara::Ambiguous
    element = first(locator)
  end
  return element
end

EDIT: It turns out that you can just use all(selector).first because find uses all under the hood anyway.

checking for disabled elements

We had a step to ensure the button was disabled which had to be changed. Below is the before and after. If there is a better way of doing this let me know!

-Then /^the "([^\"]*)" button is disabled$/ do |title|
-  find_button(title)["disabled"].should_not == nil
-end
+Then /^the delete button is disabled$/ do
+  first(".place_action input[type=\"submit\"][value=\"Delete →\"]")[:disabled].should == "true"
+end

rack_server witin driver

We have a step which was getting rack server mappings and port number from Capybara rack_server accessor. They were changed to the following respectively.

#from
Capybara::current_session.driver.rack_server.app.instance_variable_get(:@mapping)
#to
Capybara::current_session.driver.app.instance_variable_get(:@mapping)

#from
page.driver.rack_server.port
#to (Not backwards compatible)
page.server.port

The biggest chunk of work was fixing up the hundereds (exaggeration) of ambiguous errors that we were getting. Thankfully each fix was easily backported so I didn’t end up with a massive change set locally or sitting on an ever aging branch.

Hope this helps people with their upgrade

-Matt

Upgrading delayed job 3.0.4

Ran into this one at work today.

Ugpraded delayed_job gem from 3.0.3 to 3.0.4 and started getting the following error when trying to queue up jobs

Delayed::Job.enqueue(MyJob.new())
# BARF
ActiveRecord::UnknownAttributeError (unknown attribute: queue)

Someone upgraded us to 3.0.0 and missed the post install instructions of running rails generate delayed_job:upgrade which adds the queue coloumn to the delayed tasks table. There is a ticket here if you want more info.

Hope this comes up on google for upgrading 3.0.3 to 3.0.4 and saves you guys a few minutes.

-Matt

Choosing a web server

Doing some work with HTML5 WebSockets ATM and been looking at the different web servers, found this great post on the web and thought I would share.

Choosing the right one

The choice is mainly made by the server and middleware you use:

  • Multi-Process, non-preforking: Mongrel, Thin, WEBrick, Zbatery
  • Multi-Process, preforking: Unicorn, Rainbows, Passenger
  • Evented (suited for sinatra-synchrony): Thin, Rainbows, Zbatery
  • Threaded: Net::HTTP::Server, Threaded Mongrel, Puma, Rainbows, Zbatery, Thin[1]

[1] since Sinatra 1.3.0, Thin will be started in threaded mode, if it is started by Sinatra (i.e. with ruby app.rb, but not with the thin command, nor with rackup).

Remove Sensitive Data From Git

Found a great post on removing sensitive data from Git.

It makes your n00b mistakes disappear :D

https://help.github.com/articles/remove-sensitive-data

-Matt

Upgradinate

Staying on master while doing heavy upgrades

Upgradinating is a process we have used a few times at Lonely Planet now that’s saved us a lot of time and grief.

We have recently completed a few technical milestones that have been haunting us for some time:

  • Upgrading from Rails 2.3.x to Rails 3.2.x
  • Upgrading from Ruby 1.8.7 (REE) to Ruby 1.9.3

Having issues in the past with large chunks of work and a strong desire not to branch, we wanted a way we could keep shipping code without interrupting or slowing down our releases. We came up with the idea of forward-porting what we could and scripting up all other changes - and we called it upgradinating.

Enough talk, time for the code!

Simple demo

upgradinate_script.rb

  helper = UpgradinateFileHelper.new 'OLD NESS', 'NEW-NESS'
  helper.upgradinate_files [
      "/path/to/project/Gemfile"
    ]

Gemfile

  # NEW-NESS gem 'awesome_thing_to_put_in'
  gem 'deprecated_badness' # OLD NESS

Usage

ruby upgradinate_script.rb

NOTE:

  • You may need to update grep to use ‘–exclude-dir’
  • Make sure upgradinate_script.rb is executable
  # Mac OSX
  brew install xz #required for installing grep through homebrew
  brew install https://raw.github.com/Homebrew/homebrew-dupes/master/grep.rb

Examples

Why this works

  • You don’t disrupt the rest of the team’s development work
  • Easy to test your changes, can setup a CI build on old and new versions
  • You’re changes are visible to the rest of the team, less conflicts
  • Ability to push your changes quickly

More reading

  • [Branching by abstraction] (http://paulhammant.com/blog/branch_by_abstraction.html)

Update grep on Mac OSX

Update grep to the latest version using brew on mac!

OSX 10.7 comes installed with grep version 2.5.1 which sadly does not include the –exclude-dir option. To update run the following code

  brew install xz #required for installing grep through homebrew
  brew install https://raw.github.com/Homebrew/homebrew-dupes/master/grep.rb

Enjoy :)

Capybara webkit gem on Ubuntu

Hey Guys,

Tried installing the capybara-webkit gem today on Ubuntu (Precise 12.04.1 LTS (GNU/Linux 3.2.0-32-virtual x86_64)) and ran into the following error

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

I needed to install the libqt4-dev package, after running the following my problems were gone :)

sudo apt-get install libqt4-dev

-Matt

Ruby performance

G’day guys,

Had YOW! this week in Melbourne and it was amazing as always, big thanks to Lonely Planet for giving me the opportunity to go!

Found this awesome post on how to squeeze extra performance out of Ruby. Planning on stepping through the guide with my blog app, will let you know if I run into any problems :)

Also check out Zeus for those who haven’t heard of it, boot any rails app in under a second.

-Matt

RSS Feed

G’day Guys,

Finally got off my butt and created an RSS feed :)

Public transport makes me more productive :D

-Matt

Using GitHub with multiple SSH keys

Great for managing work and personal accounts

Create two keys

$ ssh-keygen -t rsa -C "your_email@youremail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/mriddle/.ssh/id_rsa): id_rsa.work

The keys I created were:

~/.ssh/id_rsa.work
~/.ssh/id_rsa.home

If you’re starting fresh clear any previously cached keys

$ ssh-add -D

Add your new SSH keys

$ ssh-add ~/.ssh/id_rsa.work
$ ssh-add ~/.ssh/id_rsa.home

Make sure they’re added

$ ssh-add -l

Update the config to use the new keys

# In this configuration the work key will be my default for github and home is my secondary
# more on this below
$ vi ~/.ssh/config

Host github.com-home
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa.home

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa.work

IdentityFile ~/.ssh/id_rsa.work
IdentityFile ~/.ssh/id_rsa.home

Update the home git repo to make use of our key by adding it to the repo git config

$ .git/config
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com-home:mriddle/blog.git

Note: You only need to update the git config with key we setup as our secondary, in this case my home key. I can leave the .git/config alone for work projects.

Hope this helps :)

-Matt

Retrieving DB connection from Rails App on Heroku

G’day,

Ran into this one today at LP while attempting to get one of our applications out on Heroku. We are using a fairly old gem called spatial_adapter which is no longer under active development.

We were failing to ‘get it up’ (ha!) due to it barfing while setting up the spatial_adapter railtie.

The stack trace we were getting was

/app/vendor/bundle/ruby/1.9.1/bundler/gems/spatial_adapter-d64ac1c03c83/lib/spatial_adapter/railtie.rb:5:in `block (2 levels) in <class:Railtie>': undefined method `[]' for nil:NilClass (NoMethodError)

I forked the repo and updated the railtie to grab the configuration in the correct manner

#Busted
ActiveRecord::Base.configurations[Rails.env]['adapter']
#Works
Rails.configuration.database_configuration[Rails.env]['adapter']

Fixed! Although I don’t know how many others will run into the same problem :)

-Matt

Ruby 1.9 and SASS character encoding issues

G’day guys,

We have just recently upgraded one of our projects to Ruby 1.9.3 from ree 1.8.7 and had a character encoding issue with a bullet point ‘•’ in our SASS.

By adding the following to our code the problem was resolved :)

#environment.rb
  Encoding.default_internal = Encoding::UTF_8
  Encoding.default_external = Encoding::UTF_8
	
#my_sass_file.sass
  @charset "UTF-8"

-Matt

Using an X11 session as another user

Hey guys,

Ran into this one today where I wanted to run a cucumber test from one of our remote agents on AWS.

I was ssh’ing into it as ubuntu and then swapping the user sudo su jenkins-node and running my feature.

I kept getting the following error

X11 connection rejected because of wrong authentication.

This was fixed by running the following

#As ubuntu user
xauth list|grep `uname -n`
#Copy the hex key

sudo su jenkins-node
DISPLAY=:0; export DISPLAY
xauth add $DISPLAY . hexkey

I was then able to run X as jenkins-node :)

Hope this helps others

-Matt

Sneaky Ubuntu on AWS

Quick one,

Just tried to attach a volume and was confused when AWS said it was attached. I had a look at my devices cat /proc/partitions and couldn’t see it.

Turns out /dev/sdf is the external name and /dev/xvdf is the internal name

#noob /facepalm

This was triggered by this post on migrating an AMI from one AWS region to another.

Moving to AP-Southeast-2 a.k.a Sydney!!!!!

-Matt

Postgres on OSX lion

Mac OSX Lion comes with postgres (9.1) which will you shouldn’t use because you will run into permission related issues down the track.

To get around this update your path and install postgres as shown below:

Installing PostgreSQL (9.2)

echo 'export PATH="/usr/bin/local:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
brew install postgres
echo "Life complete"

-Matt

Upgrading Rails 2 to 3

Upgrading from rails 2.3 to 3.x at Lonely Planet and have come across a few problems/tips which I thought would be useful to share.

RSpec matchers

with_tag & have_tag matchers are gone as of RSpec 2.

Solution: Install “rspec-html-matchers”:https://github.com/kucaahbe/rspec-html-matchers Notes:

  • Usage changed slightly. See example below
  • No longer works against XML with CDATA
# HTML
# Rails 2.x /w RSpec 1.x
  with_tag('review summary', '&lt;p&gt;Lots of fun, especially the B&amp;B&lt;/p&gt;')
# Rails 3.x /w rspec-html-matchers
  with_tag('review summary', '<p>Lots of fun, especially the B&B</p>')
# The solution wasn't obvious because the failure message would be:
  '&lt;p&gt;Lots of fun, especially the B&amp;B&lt;/p&gt;' != '&lt;p&gt;Lots of fun, especially the B&amp;B&lt;/p&gt;'
# XML
# Rails 2.x /w RSpec 1.x
 with_tag('review_summary', '<p>Lots of fun, especially the B&B</p>')
# Rails 3.x /w rspec-html-matchers
  @xml.should have_text("<review_summary><![CDATA[<p>Lots of fun, especially the B&B</p>]]></review_summary>")

I have a heap more and when I get time I will throw them up.

-Matt

Sinatra App Template

Evening guys,

Created a basic Sinatra template as I couldn’t find one with everything in it. Have put it up on “Github”:https://github.com/mriddle/sinatra_template feel free to fork it.

It comes integrated with:

  • SASS
  • HAML
  • Coffee-script
  • Sprockets 2 (asset pipeline)
  • Underscore & Backbone JS
  • RSpec2
  • -MongoDB-
  • PostgresSQL

Why PostgresSQL and not MongoDB?

TL;DR; The right tool for the job

I have used MongoDB in other projects (Java, way back) and I thought I would give it a go with this template/project I am currently working on since every other project I have done (Ruby) has been backed by PostgresSQL. However I am serious about the performance and scalability of this project and I feel that MongoDB won’t give me what I am looking for especially with the performance enhancements that came out in Postgres 9.2. Soon to take over the world, I’m sure :)

There are a few great articles floating around on the web, I think this “stack overflow question”:http://stackoverflow.com/questions/4426540/mongodb-and-postgresql-thoughts best sums it up

You dumped a decades-tested, fully featured RDBMS for a young, beta-quality, feature-thin document store with little community support. Unless you’re already running tens of thousands of dollars a month in servers and think MongoDB was a better fit for the nature of your data, you probably wasted a lot of time for negative benefit. MongoDB is fun to toy with, and I’ve built a few apps using it myself for that reason, but it’s almost never a better choice than Postgres/MySQL/SQL Server/etc. for production applications. </br> – Stack Overflow

I’d love to hear your thoughts if you disagree.

If I get some more time this week I might add Jasmine-Coffee & AngularJS.

-Matt

invalid byte sequence US ASCII

Hit my blog this morning and was very supprised to see it busted It was complaining about invalid byte sequence in US-ASCII textile_doc.rb.

Not sure what triggered the error because it has been fine for the past week without anyone touching it…

After some quick googling I was able to find the solution. Add UTF-8 as your default encoding. You can either do this in your environment.rb or config.ru with the following lines:

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

After adding the lines and running a cap deploy prod was fixed :)

-Matt

Objects on rails

Hey guys,

Reading an awesome book at the moment and I highly recommend it. It’s called ‘Objects on Rails’. You can read it for free “here”:http://objectsonrails.com/ or buy it ($5) to get the DRM-free PDF, EPUB, and Kindle versions of the book.

Here are a few awesome snippets

Posts vs. Entries. Hold on a sec. Aren’t we getting our terms confused here? First we said a Blog has “entries”. But then we started talking about “posts”. Shouldn’t we pick one or the other?

In fact, this choice to use multiple terms is deliberate. The dark side of having sensible framework conventions is that after a while, those conventions turn into assumptions. In this case, if we called the entries collection posts instead, there’s a good chance we’d start mentally conflating it with the Post class. Anything in blog.posts is a Post object, end of story.

This is one of those subtle assumptions that can lead to big problems. For instance, if we assume blog.new_post is equivalent to Post.new, we might start to just skip the Blog part and write Post.new(…) or Post.create(…) whenever we want a new blog entry.

Now imagine some time passes, and we add the ability for a Blog to have many different types of posts—photos, embedded videos, etc, each represented by different classes such as PhotoPost and VideoPost. A call to Blog.new_post(…) looks at the arguments arguments and chooses the right type of object to instantiate. Unfortunately, we hard-coded references to Post everywhere, and now we have to go back and change them all. </br> – Objects on rails

-Matt

Rename multiple files easily

Nifty script to rename multiple files with a given extension

for name in `ls *.old_extension` do 
  name1=`echo $name | sed -e 's/^\(.*\)\.old_extension$/\1\.new_extension/g'` 
  mv $name $name1
done

-Matt

Capistrano and Git permissions denied. Publickey

Morning all,

Wasted a little bit of time on this one this morning. Trying to ship some code to production using capistrano and git kept getting the following error

Permission denied (publickey)

It annoyed me because I was following the git documentation and included the following options in my delpoy.rb

#Tell cap your own private keys for git and use agent forwarding with this command.
ssh_options[:forward_agent] = true

# Must be set for the password prompt from git to work
default_run_options[:pty] = true  
                                  
set :repository, "git@github.com:user/repo.git"  # Your clone URL
set :scm, "git"
set :user, "deployer"  # The server's user for deploys

I also tested my local key to be sure I wasn’t crazy using.

$ ssh -vT git@github.com

Turns out I needed to run the following in order to register my public key for forwarding. This really ought to be added to GitHub guides IMO.

$ ssh-add

After that I was shipping like a baws (read boss)

-Matt

Awesome OSX window management

Found a decent window management app that’s free and open source . It’s called Shiftit and is a great alliterative to Divy, SizeUp, etc.

Although it hasn’t been updated in a while it works fine on 10.7 Lion. Not sure about Mountain Lion, haven’t tried.

Download it here

-Matt

Removing multiple dead symlinks via command line

Stumbled upon this one today, love it! Easily remove all broken sym links in the current directory.

$ find -L -maxdepth 1 -type l -delete

Breakdown:

The “-L” option means to always follow symbolic links The “-type l” will return true when “-L” is in effect only when the symbolic link is broken. The “-maxdepth” options means only look in the given directory The “-delete” deletes the file.

-Matt

Enable VNC access on OSX via command line

Hey folks,

Here is are a few steps to enable VNC access on your mac. Run this on the command line via SSH if your stuck externally :)

cd /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/
sudo kickstart -configure -allowAccessFor -allUsers -privs -all
sudo kickstart -configure -clientopts -setvnclegacy -vnclegacy yes 
sudo kickstart -configure -clientopts -setvncpw -vncpw password
#Restart
sudo kickstart -restart -agent -console
#Done, VNC GO

-Matt

Adding the Reveal-in-sidebar shortcut to Sublime Text 2

Most users are making the change from TextMate to Sublime, one of the cool shortcuts TextMate has is ‘Reveal in Sidebar’

Sublime doesn’t come with this out of the box but it’s simple enough to add

Under Sublime Preferences > Key Bindings, add the following line

{ "keys": ["super+ctrl+r"], "command": "reveal_in_side_bar"}

Enjoy!

-Matt

Nifty unix command

Here’s a quick one for ya, execute a previously run command

$ history | grep firefox
  176  /Applications/Firefox.app/Contents/MacOS/firefox
  177  /Applications/Firefox.app/Contents/MacOS/firefox
$ !176
/Applications/Firefox.app/Contents/MacOS/firefox

Simple and nifty enough ay, grep history for w/e you’re looking for and then bang the number :D

-Matt

Clearing Shell Buffer in UNIX

G’day

Helpful hint for the day. To clear the terminal buffer/screen in iTerm on Mac OS X hit the following keys

Apple+K

Ta-da

-Matt

OSX Lion MacFusion MacFuse and libfure.0.dylib-error

I ran into this problem using Mac Fusion on OSX Lion.

dyld: Library not loaded: /usr/local/lib/libfuse.0.dylib
Referenced from: 
/Applications/Macfusion.app/Contents/PlugIns/sshfs.mfplugin/Contents/Resources/sshfs-static
Reason: image not found

The Library “libfuse.0.dylib” doesn’t exist. With Lion being x64 and MacFuse no longer being maintained.

Have no fear though, there is a 64 bit version via Tuxera and works.

You can downloaded it here

Jasmine with Jammit error xyz is not a constructor

G’day guys, long time no post.

Ran into this one a few times and every now and then I forget the answer.

When doing TDD development working with javascript/coffee & jasmine /w jammit I sometimes run into “‘some class bla’ is not a function” and that’s because the class isn’t defined. The reason for this is because the newly created file that I am TDD’ing has NOT being included in my assets.yml

Solution: Include the damn file you’re testing in config/assets.yml fool!

Remove trailing whitespace on save (Sublime Text 2)

Hey peeps,

Sublime text 2 comes with the ability to remove trailing whitespace on save but is disabled by default. To enable it simply open Sublime Preferences > Settings - User and change trim_trailing_white_space_on_save

// Set to true to removing trailing white space on save
"trim_trailing_white_space_on_save": true,

DEATH TO ALL WHITESPACE

-Matt

TextMate UTF8 does not map to unicode

Hey Guys,

I ran into a problem today working with TextMate where my search wouldn’t complete and I would get a whole lot o red errors about file encoding

The error message wasn’t very clear but, with a minor modification of ackmate_ack you can get it to print each file it searches which will help you identify where the problematic files are.

mate /Users/dev/Library/Application\ 
   Support/TextMate/PlugIns/AckMate.tmplugin/Contents/Resources/ackmate_ack

and added this on line 1560

App::Ack::warn( "$filename" );

Run a search. At the end of the search output, all searched files are now listed and the ones followed by the error are the ones that are not UTF8 encoded.

My problem was a bunch of old tmp cache files that were left lying around in my project dir. I deleted them and didn’t have anymore issues.

Hope that helps!

-Matt

Testing offline apps with Selenium

G’day peeps,

Had an issue today running our offline cucumber tests with Selenium and Firefox. The message “This website (127.0.0.1) is asking to store data on your computer for offline use.” kept coming up with firefox version 7+. To allow it I made the following modification to our firefox profile.

First create a custom profile (selenium_firefox_profile) with an sqllite DB.

$ mkdir selenium_firefox_profile
$ cd selenium_firefox_profile
$ sqlite3 permissions.sqlite3 # This filename is important
sqlite> CREATE TABLE moz_hosts ( id INTEGER PRIMARY KEY,host TEXT,type TEXT, 
permission INTEGER, expireType INTEGER, expireTime INTEGER);
sqlite> INSERT INTO "moz_hosts" VALUES(1,'127.0.0.1','offline-app',1,NULL,NULL);
sqlite> .quit

Setup Cabybara with your new custom profile

Capybara.register_driver :selenium_firefox_profile do |app|
  require 'selenium/webdriver'
  profile_path = "/path/to/selenium_firefox_profile"
  profile = Selenium::WebDriver::Firefox::Profile.new(profile_path)
  driver = Capybara::Driver::Selenium.new(app, :profile => profile)

  driver
end

# And set the default driver to our new one:

Capybara.default_driver = :selenium_firefox_profile

Everything should be gravy :)

-Matt

Mac keyboard shortcuts

Nice reference for Mac shortcuts. Unleash your inner power user.

Includes 10.7 Lion, 10.6 Snow Leopard, 10.5 Leopard and 10.4 Tiger

http://www.danrodney.com/mac/

Enjoy

-Matt

Text selection in Chrome using javascript

Hey guys

Just a quick one that I got stuck on at work.

TL;DR The text needs to be visible on the page (i.e. display:block) for you to select it in Chrome. In Firefox it only needs to be visible in the DOM

Text selection code example:

var range, selection;
expect(startElement).toBeTruthy();
expect(endElement).toBeTruthy();
selection = window.getSelection();
if (selection.rangeCount > 0) {
  selection.removeAllRanges();
}
range = document.createRange();
range.setStart(startElement, startOffset);
range.setEnd(endElement, endOffset);
selection.addRange(range);

The selection.isCollapsed property defines the cursor as having some text highlighted (true/false). In Chrome you can only highlight whats visible on the page. In Firefox you can highlight any element that resides within the DOM. It makes sense for you to only be able to highlight text that’s visible but if, for example, you had Jasmine tests, and you have it hidden in a fixture because you want to see the results of the test. It won’t work… (in Chrome) unless it’s visible.

Long story short. Make sure you set the element to be visible in your sass/css

Apologies if it was chaotic to read, it’s very much a brain dump.

-Matt

Segmentation fault while installing REE on OSX Lion

Hey guys, it’s been a while I know! Started a new job at Lonely Planet which has kept me pretty busy. Came across error below while setting up Ruby Enterprise Edition (REE)

cc -g -O2  -pipe -fno-common    -DRUBY_EXPORT  -L.    
  main.o dmydln.o libruby-static.a -L/opt/local/lib -Wl,
  -rpath,/Users/mojo/.rvm/rubies/ree-1.8.7-2011.03/lib -L/Users/
   mojo/.rvm/rubies/ree-1.8.7-2011.03/lib -lsystem_allocator -ldl -
   lobjc   -o miniruby
ld: warning: directory not found for option '-L/opt/local/lib'
./ext/purelib.rb:2: [BUG] Segmentation fault
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin11.1.0], 
  MBARI 0x6770, Ruby Enterprise Edition 2011.03

make: *** [.rbconfig.time] Abort trap: 6

Basically Xcode 4 on OSX Lion 10.7 defaults CC to llvm-gcc-4.2 which causes error in compiling REE under RVM. So when you install REE you might bump to following error

The solution is simple

$ export CC=/usr/bin/gcc-4.2
$ rvm install ree --force

Happy Tuesday -Matt

RedCloth does not install on Ruby 1.9.3-preview1

Hey guys,

Installing my RoR environment on my laptop and thought I would give Ruby 1.9.3-preview1 a whirl (via RVM). When running

$ bundle install

I ran into an error installing RedCloth. Below is an extract

make
compiling redcloth_inline.c
cc1: warnings being treated as errors
ragel/redcloth_inline.c.rl: In function ‘red_parse_title’:
ragel/redcloth_inline.c.rl:68:7: error: ISO C90 forbids mixed declarations and code
ragel/redcloth_inline.c.rl: In function ‘red_block’:
ragel/redcloth_inline.c.rl:99:3: error: ISO C90 forbids mixed declarations and code

If you want to install the RedCloth gem then run the following command

gem install RedCloth -- --with-cflags="-std=c99"

Note: I also had the same issue with the gherkin-2.3.3 gem.

Hope it helps others out.

-Matt

Searching through Terminal history

Hey Everyone,

Stumbled upon a neat tip today. You can search through your Terminal history using the everse-i-search feature by hitting [ctrl]+r which also works on Mac :D

By doing this you should see the following

(reverse-i-search):

Start typing and it will find previous commands, if there are multiple matches you can hit [ctrl] + r again to cycle through the results. Hit enter to use the command or the left arrow to select it if you wish to modify the command before running it.

Enjoy!

-Matt

How to set a static IP in Ubuntu from the shell

Just a quick one this morning. Below are simple instructions on how to set a static IP on Ubuntu from the shell. I am sure the instructions can be used on any Debian based system.

Open interfaces with your favourite editor

sudo vi /etc/network/interfaces

Modify the lines under #The primary network interface to match your requirements. In this example I use 192.168.0.10

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
        address 192.168.0.10
        netmask 255.255.255.0
        network 192.168.0.0
        broadcast 192.168.0.255
        gateway 192.168.0.1

Save the file -and reboot the system, the changes will be applied.- EDIT: (01/08/2011 @ 9:11 PM) You never need to reboot, in the above example, after you save you can do the following to apply the changes (courtesy of Mat)

$ sudo /etc/init.d/networking restart

Bonus Points (A.K.A Tips)

If you’re only making a temporary change to eth0 (for testing etc..) do (courtesy of Mat)

$ sudo ifconfig eth0 192.168.0.10 netmask 255.255.255.0

-Matt

Installing ActiveMQ as a service on Linux

Description

Apache ActiveMQ is a complete message broker and full JMS 1.1 provider featuring clustering, distributed destinations and XA support with pluggable persistence (JDBC, BDB, JDBM) and transport layers (TCP, UDP, multicast, NIO, SSL, Zeroconf, JXTA, JGroups).

Installation

This installation was done on Ubuntu x64, may be different for other Linux distributions. The instructions will vary a little depending on system architecture (32 or 64 bit)

If you haven’t done so already download ActiveMQ from here extract the download to a directory of your choice. I placed mine in /usr/local. The rest of the guide will assume it’s in /usr/local, the full path of my installation is /usr/local/apache-activemq-5.5.0

Configuration

Open activemq and set ACTIVEMQ_HOME to point to your installation directory

$ sudo vi /usr/local/apache-activemq-5.5.0/bin/linux-x86-64/activemq

In activemq

ACTIVEMQ_HOME="/usr/local/apache-activemq-5.5.0"

Save activemq and open wrapper.conf, change set.default.ACTIVEMQ_HOMEI and set.default.ACTIVEMQ_BASE to point to your installation directory

$ sudo vi /usr/local/apache-activemq-5.5.0/bin/linux-x86-64/wrapper.conf

In wrapper.conf

set.default.ACTIVEMQ_HOME=/usr/local/apache-activemq-5.5.0
set.default_ACTIVEMQ_BASE=/usr/local/apache-activemq-5.5.0

Save wrapper.conf and create a soft link in init.d

$ sudo ln -s /usr/local/apache-activemq-5.5.0/bin/linux-x86-64/activemq /etc/init.d/activemq

Note: When creating a soft link make sure it’s the full path even if your currently in that directory. I didn’t and I had issues making one.

Update rc.d

$ sudo update-rc.d activemq \ [hit_enter]
 	 start 66 2 3 4 5 . stop 34 0 1 6 .

And you’re done.

Bonus points

Start or stop the service manually

$ service activemq start
$ service activemq stop

Check if ActiveMQ is running

$ service activemq status

Uninstalling the service

$ sudo update-rc.d -f activemq remove
$ sudo rm /etc/init.d/activemq

If you had any problems let me know

-Matt

Chkconfig in Debian based environments

I tried to use chkconfig today and found it wasn’t included with my vanilla Ubuntu install. To install it simply run the following command

$ sudo apt-get install chkconfig

OR you can download it from here

An explanation of chkconfig

A system tool to enable or disable system services

Chkconfig is a utility to update and query runlevel information for system services. Chkconfig manipulates the numerous symbolic links in /etc/init.d/, to relieve system administrators of some of the drudgery of manually editing the symbolic links.

This package also contains the program “service” for running init scripts in a predictable environment.

In Debian, there are several tools with similar functionality, but users coming from other Linux distributions will find the tools in this package more familiar.

Hope it helps folks out

-Matt

Running VisualVM through an SSH tunnel

So I had troubles connecting to my Java app on Amazon EC2 due to the firewall. I modified the security group to allow the default port of 1099 but was still unable to connect to due the random port opened and used by RMI (Remote Method Invocation). There was no way I was going l to open up all TCP ports so I could monitor my app remotely so I decided to go down a different path.

To connect to your app via an SSH tunnel on Linux simply do the following. In this example I use 9696 as my port, you can change it if you like. For windows I suggest you use putty or openssh! Or just roll Linux :P

Connect to your remote machine where your Java Application is running

$ sudo ssh -D 9696 your.ip.goes.here -i .ssh/yourkeyfileifyougotone.pem

As in the previous example make sure you create a file called jstatd.all.policy in $JAVA_HOME/bin with the following contents:

grant codebase "file:${java.home}/../lib/tools.jar" { permission java.security.AllPermission;};

Run jstatd using the following command (within the server terminal)

$ sudo jstatd -p 1099 -J-Djava.security.policy=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/
 	 bin/jstatd.all.policy &

Then simply run VisualVM from your desktop running the following command

Linux

$ jvisualvm -J-Dnetbeans.system_socks_proxy=localhost:9696 -J-Djava.net.useSystemProxies=true

Windows

visualvm.exe -J-Dnetbeans.system_socks_proxy=localhost:9696 -J-Djava.net.useSystemProxies=true

That’s it!

If it doesn’t work or you did something different let me know. So we can share the knowledge.

-Matt

It's slash admin not slash console

I think I am late to the party with this discovery.

If you use "mstsc /v:SERVERNAME /console" in a script file or via start>run then the /console will be ignored (with no warning) and you will be connected to a normal session that is not the server console.

For Windows Server 2008, Vista & I believe Windows 7 the flag is now /admin instead.

Enabling a surface on an OSAR

This could be off topic for people that follow my blog but it recently came up and I thought it was worthwhile sharing the solution.

This is related to the IBM FileNet OSAR (Optical Storage and Retrieval) unit, a robotic optical storage jukebox storing up to 64 2.6-gigabyte optical disks along with a complement of drivers supporting server peripherals.

There was an issue retrieval documents, after some investigation I discovered that one of the surfaces was disabled for reads. I originally tried to simply re-enable it using xapex (the user interface for the OSAR) but nothing happened and nothing was shown in the logs either. Turns out you need to disable both sides to the disk before you can enable it for read/write.

So if anyone out there also happens to be troubleshooting why documents aren’t being retrieved because the surface is disabled for reads then simply disable the other side/surface and enable both for read/write. It’s most likely not a common problem that the average developer/consultant has though.

-Matt

Monitoring Remote Java Applications with VisualVM

Hey guys, the blog updates have slowed since I found Minecraft. Fairly addictive. Just a quick one today.

To monitor a remote Java application with VisualVM, simply use jstatd

Start jstatd on the remote machine, make a connection from VisualVM to the remote machine and we can then monitor all running Java applications.

If you try starting jstatd without specifying a policy you may get the following error.

$ ./jstatd
Could not create remote object
access denied (java.util.PropertyPermission java.rmi.server.ignoreSubClasses write)
java.security.AccessControlException: access denied 
                (java.util.PropertyPermission java.rmi.server.ignoreSubClasses write)
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:342)
	at java.security.AccessController.checkPermission(AccessController.java:553)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
	at java.lang.System.setProperty(System.java:744)
	at sun.tools.jstatd.Jstatd.main(Jstatd.java:139)

Create a security policy file if one does not already exist called ‘jstatd.all.policy’ (same location as jstatd). Add the following to the file:

grant codebase "file:${java.home}/../lib/tools.jar" {

   permission java.security.AllPermission;

};

Then simply run jstatd with:

$ ./jstatd -J-Djava.security.policy=jstatd.all.policy &

You can test the connection using the following command

$ jps -l -m -v rmi://localhost

If you’re experiencing problems make sure you run jstatd with the same user as the java processes. It could be that the ports used my jstatd are blocked by your firewall. jstatd uses two ports, the once specified and a random port. Check the ports by running the following command

$ netstat -nap | grep jstatd
tcp        0      0 :::47232    :::*   LISTEN      23185/jstatd        
tcp        0      0 :::1099      :::*     LISTEN      23185/jstatd  

EDIT: You can find the log files for visualvm here

Linux

[userdir]/.visualvm/[version]/var/log/messages.log

Windows

C:\Users[username]\AppData\Roaming.visualvm[version]\var\log\messages.log

-Matt

Some useful Java Keytool commands

Below are some commands you might find useful in determining Keystore issues.

Deleting a certificate

sudo keytool -delete -keystore /usr/lib/jvm/java-6-sun-1.6.0.06/jre/lib/security/jssecacerts
 	 -alias aliasname

List all of the keystore certificates

keytool -list -v -keystore keystorelocation | more

List all cacerts certificates

keytool -list -keystore /etc/java-6-sun/security/cacerts | more

List or display a certificate

keytool -printcert -v -file anycert.cer | more

Hope they come in handy

-Matt

Rerun the last command with sudo in unix

Have you ever executed a command in Unix and noticed the that you had to run it with sudo? Instead of typing the command again with “sudo” in front of it, just run:

$ sudo !!

EDIT (05/06/2011 @ 10:20 PM) Mathew pointed out that running

$ !! command

will put the command at the end of the line. For example

$ rails
$ !! server
#output
$ rails server

Basically the !! is a replacement for the previous line entered in the Linux shell.

Update: You can also use !$ for partial replacement. For example

$ mkdir temp_folder_to_delete
$ rm -rf !$ #short for rm -rf temp_folder_to_delete

-Matt

Please install the postgres adapter

I ran into the following problem this evening when starting my rails server

$ rails server
=> Booting WEBrick
=> Rails 3.0.4 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/local/rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.4/lib/active_record/connection_adapters
/abstract/connection_specification.rb:71:in `rescue in establish_connection':
Please install the postgres adapter: `gem install activerecord-postgres-adapter`
(no such file to load -- active_record/connection_adapters/postgres_adapter) (RuntimeError)

I got this error on my laptop so I thought I had some missing dependencies, I ran the gem install command as it suggested but got the following as a response:

$ gem install activerecord-postgres-adapter
ERROR:  Could not find a valid gem 'activerecord-postgres-adapter' (>= 0) in any repository
ERROR:  Possible alternatives: activerecord-postgis-adapter, activerecord-postgres-copy,
activerecord-postgres-hstore, activerecord-jdbcpostgresql-adapter, activerecord-dbslayer-adapter

There were a few suggestions on StackOverflow, they suggested installing the postgres gem

$ gem install pg

However I already had it installed. Ensured I had the latest version and kept looking…

The solution was simple really, I needed to change my database.yml file. I had the adapter set to postgres instead of postgresql

Newb mistake I will not be making again.

-Matt

Using MongoHQ for logging with Heroku

Free logging on Heroku is limited, with basic giving you 500 lines of history and expanded giving you the ability to tail. If you want to keep anymore then you have to upgrade for $10 a month, which aint bad if your running something that generates money.

For my blog however I wanted to keep the last week or so of logs and not pay anything so I setup logging with MongoHQ and a gem called central_logger. MongoHQ offer a free shared DB of 16MB which is more than enough for me. Central Logger is a centralized logging gem for rails apps with MongoDB which includes support for Rails 3 and 2.

Setting it up

  • Create an account on MongoHQ “here”:https://mongohq.com/signup
  • Install the gem. Add the following to your Gemfile and run bundle install or run gem install central_logger
  #Gemfile
    gem "central_logger"

  #applicatication_controller.rb
    include CentralLogger::Filter
  • If using Rails 3 SKIP this step, otherwise add the following to config/environment.rb

require 'central_logger' CentralLogger::Initializer.initialize_deprecated_logger(config)

  • Add mongo settings to database.yml for each environment in which you want to use the Central Logger. The central logger will also look for a separate central_logger.yml or mongoid.yml (if you are using mongoid) before looking in database.yml. In the central_logger.yml and mongoid.yml case, the settings should be defined without the ‘mongo’ subkey.
#database.yml
development:
  adapter: mysql
  database: my_app_development
  user: root
  mongo:
    # required (the only required setting)
    database: my_app
    # default: 250MB for production; 100MB otherwise - MongoHQ free account caps out at 16MB
    capsize: <%= 10.megabytes %>
    # default: localhost - May not be flame.mongohq, may be one of there other subdomains
    host: flame.mongohq.com
    # default: 27017
    port: 27099
    # default: false - Adds retries for ConnectionFailure during voting for replica set master
    replica_set: true
    # default: false - Enable/Disable safe inserts (wait for insert to propagate to all nodes)
    safe_insert: true
    # default: Rails.application - Only really needed for non-capistrano Rails 2 deployments
    # Otherwise should set automatically.
    application_name: my_app
    # MongoHQ DB username
    username: myusername
     # MongoHQ DB password
    password: mypass

OR

#central_logger.yml
development:
    database: my_app
    capsize: <%= 10.megabytes %>
    host: flame.mongohq.com
    port: 27099
    replica_set: true
    username: myusername
    password: mypass

#test:
#......

#production:
#......

Remember that people may have access to this file, so storing the DB password in here is not the best idea.

That’s it, restart the server and you’re good to go. Once the page hits start coming in you will see a new collection developer_log (or w/e you set it up for) with documents being created per request. The log format will look like this

{
   'action'           : action_name,
   'application_name' : application_name (rails root),
   'controller'       : controller_name,
   'ip'               : ip_address,
   'messages'         : {
                          'info'  : [ ],
                          'debug' : [ ],
                          'error' : [ ],
                          'warn'  : [ ],
                          'fatal' : [ ]
                        },
   'params'           : { },
   'path'             : path,
   'request_time'     : date_of_request,
   'runtime'          : elapsed_execution_time_in_milliseconds,
   'url'              : full_url
}

If you want to add extra information to the base of the document (let’s say something like user_guid on every request that it’s available), you can just call the Rails.logger.add_metadata method on your logger like so (for example from a before_filter):

# make sure we're using the CentralLogger in this environment
if Rails.logger.respond_to?(:add_metadata)
    Rails.logger.add_metadata(:user_guid => @user_guid)
end

-Matt

Disqus kills page load times

I have been pretty busy this weekend with the site adding new features and modifying the layout. One of the new features I have added is Disqus . It’s pretty awesome, replacing the conventional comment section with a more ‘social-friendly’ version. However after implementing it I noticed my page load time went from under 1 second to 14 seconds… Firebug (Firefox web dev addon) was showing 50+ GET requests for myaccount.disqus.com/count.js with each taking 200-500ms to complete. Some quick investigation showed it was just to retrieve the comment and reaction count for each post. Not worth the wait or necessary but I still wanted to keep it.

I reduced the load times by using a nifty jQuery plugin called appear which fires when an element scrolls into view or otherwise becomes visible to the user. This effectively only loading a comment count for a post when a user scrolled down to it.

Once I added the jQuery plugin I made a simple modification to the code Disqus provides.

From

#script type="text/javascript"
  /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
  var disqus_shortname = 'example'; // required: replace example with your forum shortname

  /* * * DON'T EDIT BELOW THIS LINE * * */
  (function () {
      var s = document.createElement('script'); s.async = true;
      s.type = 'text/javascript';
      s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
      (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0])
         .appendChild(s);
  }());
#/script

To

#script type="text/javascript"
  /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
  var disqus_shortname = 'example'; // required: replace example with your forum shortname

  /* * * DON'T EDIT BELOW THIS LINE * * */
  $("div#post_no_<%= post.id %>").appear(function () {
      var s = document.createElement('script'); s.async = true;
      s.type = 'text/javascript';
      s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
      (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0])
        .appendChild(s);
  });
#/script

Where $("div#post_no_<%= post.id %>") is the ID of my post or w/e the viewer will see before the script is executed.

With that simple change pushed to heroku my load times were back down to around 1-2 seconds.

Also, I stumbled upon an alternative to Firebug if you don’t have it installed. It’s a web page that is basically the same simply input the URL of the site and check out the load times. It’s called loads.in Enjoy

EDIT (04/06/2011 @ 11:09 PM) Chris Aitchison mentioned YSlow in his comments, it’s another Firefox addon that works with Firebug. I have just installed it and am impressed at how easy and useful it is. Check it out here

YSlow analyzes web pages and suggests ways to improve their performance based on a set of rules for high performance web pages. YSlow is a Firefox add-on integrated with the Firebug web development tool. YSlow grades web page based on one of three predefined ruleset or a user-defined ruleset. It offers suggestions for improving the page's performance, summarizes the page's components, displays statistics about the page, and provides tools for performance analysis, including Smush.it™ and JSLint.

-Matt

Heroku DB pull alternative

I often clone my production data to play with during development. Even though my database is small the heroku db:pull takes ages. Don’t worry though because there is a free and easy alternative (If your development database is postgres). It’s called PG Backups

To get up and running with PG Backups do the following

Install

$ heroku addons:add pgbackups

Backup

$ heroku pgbackups:capture

View

$ heroku pgbackups

Download

$ heroku pgbackups:url b001

Restore

$ pg_restore --verbose --clean --no-acl --no-owner -h myhost -U myuser -d mydb latest.dump

Delete (If you wanted to)

$ heroku pgbackups:destroy b001

If you don’t have postgres as your back-end for development I suggest you do because it’s awesome and Heroku use it as your production database anyway.

-Matt

Grouping and sorting Active Records in Ruby

I have made a few changes to my blog recently, one of them being a list of posts on the right hand side. I had some difficulties with it as I am still new to Ruby and couldn’t find any examples. So here is mine, this is a nice simple way to group Posts by year and month.

<% Post.all
        .group_by{|ar| ar.created_at.strftime "%Y %b" }
           .each do |date_str, post_for_month| %>
  <%= date_str %>
 
    <% post_for_month.each do |post| %>
  • <%= link_to(post.title, post_path(post)) %>
  • <% end %>
<% end %>

The expected output will be

2011 January

  • post title one
  • post title two

2011 February

  • post title one

If you want to sort by date as well simply change the code above to this

<% Post
       .order("created_at DESC")
         .group_by{|ar| ar.created_at.strftime "%Y %b" }
            .each do |date_str, post_for_month| %>
  <%= date_str %>
 
    <% post_for_month.each do |post| %>
  • <%= link_to(post.title, post_path(post)) %>
  • <% end %>
<% end %>

More information can be found here and here

-Matt

10 neat Ruby one liners

Marcus Kazmierczak came up with a list of 10 one-liner examples that are meant to showcase Scala’s expressiveness . A [CoffeeScript version]http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html) quickly emerged, and now a Ruby one

Multiply each item in a list by 2

(1..10).map { |n| n * 2 }

Sum a list of numbers

(1..1000).inject { |sum, n| sum + n }

Or using the (built in) Symbol#to_proc syntax that’s been available since Ruby 1.8.7:

(1..1000).inject(&:+)

Or even just passing a symbol directly:

(1..1000).inject(:+)

Verify if tokens exist in a string

words = ["scala", "akka", "play framework", "sbt", "typesafe"]
tweet = "This is an example tweet talking about scala and sbt."

words.any? { |word| tweet.include?(word) }

Reading a file

file_text = File.read("data.txt")
file_lines = File.readlines("data.txt")
The latter includes “\n” at the end of each element of the array, which can be trimmed by appending .map {strstr.chop } or by using the alternative version:
File.read("data.txt").split(/\n/)

Happy Birthday

4.times { |n| puts "Happy Birthday #{n==2 ? "dear Tony" : "to You"}" }

Filter a list of numbers

[49, 58, 76, 82, 88, 90].partition { |n| n > 60 }

Fetch and parse an XML web service

require 'open-uri'
require 'hpricot'

results = Hpricot(open("http://search.twitter.com/search.atom?&q=scala"))

This example requires open-uri and hpricot or equivalent libraries (you could use builtin ones if you wish). It’s not too much code, but Scala clearly wins here.

Find minimum (or maximum) in a list

[14, 35, -7, 46, 98].min
[14, 35, -7, 46, 98].max

Parallel Processing

require 'parallel'
Parallel.map(lots_of_data) do |chunk|
      heavy_computation(chunk)
end

Unlike Scala, multicore support is not built-in. It requires parallel or a similar gem.

Sieve of Eratosthenes

The Scala one liner is very clever, but entirely unreadable. A simpler implementation that is no longer a one-liner in Ruby would be:

index = 0
while primes[index]**2 <= primes.last
      prime = primes[index]
      primes = primes.select { |x| x == prime || x % prime != 0 }
      index += 1
end

This last example is straight from StackOverflow . Not the prettiest code ever, but you get the idea.

-Matt

Global acccess to Rake DSL methods is deprecated

You may have noticed this error recently running rake db:create or rake db:migrate… The newest version of Rake (0.9+) breaks rails.

If you are getting this error: WARNING: Global access to Rake DSL methods is deprecated. Please Include ....Rake::DSL into classes and modules which use the Rake DSL methods.

There are two ways to fix this

  • The quick fix is to just downgrade to Rake 0.8.7 by doing the following:

Add the gem version to your Gemfile

gem "rake", "0.8.7"

Remove rake 0.9.1 by running

gem uninstall rake -v=0.9.1

Update rake

bundle update rake
  • The other option is to add the following to your Rakefile above the load_tasks:
module ::YourApplicationName
   class Application
    include Rake::DSL
  end
end
module ::RakeFileUtils
  extend Rake::FileUtilsExt
end

Let me know if you’re still having problems.

-Matt

Disc or Disk?

Asked this question yourself? I certainly have. I have always used disc when referring to a CD/DVD and disk when referring to a hard drive but I never really knew if that was correct, until today, when I decided to look it up.

So for those of you interested

Disk: A disk refers to magnetic media, such as the disk in your computer’s hard drive or an external hard drive. A disk is always rewritable, unless intentionally locked. You can partition a disk into several smaller volumes, too.

A disk is non-removable.

Disc: A disc refers to optical media, such as CD-ROM or a DVD. Some discs are read only, some only allow you to write to them once and some allow you to rewrite to them multiple times.

A disc is removable.

Hope that clears things up, I found the definition here

-Matt

Save gvim colour scheme

If you use gvim and don’t like the default colour scheme, you may have noticed changes to it wont persist between sessions. Add the following line to the following file to fix this

colorscheme slate

Note: Replace slate with the theme of your choice (case sensitive)

sudo gvim ~/.gvimrc

Note: This file may not exist, simply create it if it does not

Happy coding

-Matt

Postgres fe sendauth no password supplied


fe_sendauth: no password supplied

If you’re getting this error in your local development when trying to use PostgreSQL set your pg_hba.conf to include a trust line for the hosts involved to fix the problem.

The default location for pg_hba.conf can be found here: /etc/postgresql/9.0/main/pg_hba.conf

I changed the following lines at the bottom of the file

# "local" is for Unix domain socket connections only
local   all         all                               trust
# IPv4 local connections:
host    all         all         127.0.0.1/32          trust
# IPv6 local connections:</code>
host    all         all         ::1/128               trust

Remember to restart your postgres server ;)

-Matt

It's quiet... too quiet

If you noticed it’s quiet around here, stress less, it’s not the beginning of the zombie-apocalypse, it’s just me being been busy playing with Ruby on Rails. I find the best way to learn is to just jump right in and start playing with code.

For those of you who are interested in learning Ruby on Rails I can’t recommend the following sites enough! They have been a great help.

  • Code School - RailsForZombies If you have no idea where to begin and your new on the block then this is the best place to start. Sign up (which is free) and join Gregg Pollack from Envy Labs who will walk you through the basics of learning Ruby on Rails in five levels, each of which is followed by a series of code challenges where you get to start coding Rails immediately in the browser. This course will teach you the basics of Ruby on Rails including CRUD, models, views, controllers and routing.
  • Rails Casts[ RailsCasts is produced by Ryan Bates. He offers screen-casts which are short and focus on one technique so you can quickly move on to applying it to your own project. The topics target the intermediate Rails developer, but beginners and experts will get something out of it as well.

To teach yourself Ruby on Rails I find it’s best if you pick something that’s already been done like Twitter or Amazon (only much smaller) and make your own version of it using Ruby. I decided to create my own version of IMDB and add cool new features like having your own library, friends, movie review requests, recommending media to friends, etc.

A few items I recommend taking a look at

  • Sass CSS
  • Haml (HTML Abstraction Markup Language)
  • Sinatra Web framework. Basically, it’s a library that allows you to write web applications with a minimum of red tape. You simply load Sinatra, and start coding application logic. I recommend learning Ruby without it for a little while until you get the basics down pat.
  • TextMate Easy to use text editor, if you’re using a Mac that is.

If there is anything that’s helped you while learning Ruby on Rails or any awesome tool that makes it easier or more enjoyable then please do share it in the comments section below!

-Matt

Rails Rake Tasks reference

Quick reference and explanation of rake tasks

#Clears all cached pages
rake cache:clear
#Loads a schema.rb file into the database and then loads the initial database fixtures.
rake db:bootstrap
#Copy default theme to site theme
rake db:bootstrap:copy_default_theme
#Migrate the database through scripts in db/migrate. Target specific version with VERSION=x
rake db:migrate
#Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:dump
#Load a schema.rb file into the database
rake db:schema:load
#Load initial database fixtures (in db/bootstrap/*.yml) into the current environment's database.
#Load specific fixtures using FIXTURES=x,y
rake db:bootstrap:load
#Load fixtures into the current environment's database.
#Load specific fixtures using FIXTURES=x,y
rake db:fixtures:load
#Clear the sessions table
rake db:sessions:clear
#Creates a sessions table for use with CGI::Session::ActiveRecordStore
rake db:sessions:create
#Dump the database structure to a SQL file
rake db:structure:dump
#Recreate the test database from the current environment's database schema
rake db:test:clone
#Recreate the test databases from the development structure
rake db:test:clone_structure
#Prepare the test database and load the schema
rake db:test:prepare
#Empty the test database
rake db:test:purge
#Push the latest revision into production using the release manager
rake deploy
#Describe the differences between HEAD and the last production release
rake diff_from_last_deploy
#Build the app HTML Files
rake doc:app
#Remove rdoc products
rake doc:clobber_app
#Remove plugin documentation
rake doc:clobber_plugins
#Remove rdoc products
rake doc:clobber_rails
#Generate documation for all installed plugins
rake doc:plugins
#Build the rails HTML Files
rake doc:rails
#Force a rebuild of the RDOC files
rake doc:reapp
#Force a rebuild of the RDOC files
rake doc:rerails
#Freeze rails edge
rake edge
#Truncates all *.log files in log/ to zero bytes
rake log:clear
#Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or
# a tag with TAG=Y (ex: TAG=rel_1-1-0)
rake rails:freeze:edge
#Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:freeze:gems
#Unlock this application from freeze of gems or edge and return to a fluid use of system gems
rake rails:unfreeze
#Update both configs, scripts and public/javascripts from Rails
rake rails:update
#Update config/boot.rb from your current rails install
rake rails:update:configs
#Update your javascripts from your current rails install
rake rails:update:javascripts
#Add new scripts to the application script/ directory
rake rails:update:scripts
#Execute a specific action using the release manager
rake remote_exec
#Rollback to the release before the current release in production
rake rollback
#Enumerate all available deployment tasks
rake show_deploy_tasks
#Report code statistics (KLOCs, etc) from the application
rake stats
#Test all units and functionals
rake test
#Run tests for functionalsdb:test:prepare
rake test:functionals
#Run tests for integrationdb:test:prepare
rake test:integration
#Run tests for pluginsenvironment
rake test:plugins
#Run tests for recentdb:test:prepare
rake test:recent
#Run tests for uncommitteddb:test:prepare
rake test:uncommitted
#Run tests for unitsdb:test:prepare
rake test:units
#Clears all files and directories in tmp/cache
rake tmp:cache:clear
#Clear session, cache, and socket files from tmp/
rake tmp:clear
#Creates tmp directories for sessions, cache, and sockets
rake tmp:create
#Clears all files in tmp/pids
rake tmp:pids:clear
#Clears all files in tmp/sessions
rake tmp:sessions:clear
#Clears all files in tmp/sockets
rake tmp:sockets:clear
#Copies the latest dialog.js to the application's public directory
rake update_dialog_helper

-Matt

Setting up Apache2 with the Worker MPM on Ubuntu

I hit a bottle neck the other day during development (stress-testing) and I found by modifying my Apache2 MPM I could achieve better results. By default my installation of Apache2 on Ubuntu had the Prefork Multi-Processing module configured which is great for single or dual core systems where one process handles one request at a time (no threading) and is thread safe. I wanted to utilize the resources on the box so I reconfigured my Apache2 install to use the Worker Multi-Processing Module. The Worker MPM is generally better for multi core systems.

This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with less system resources than a process-based server. Yet it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.

Lets begin Check what version you have by running the following command

$ /usr/sbin/apache2ctl -V
Server version: Apache/2.2.17 (Ubuntu)
Server built:   Feb 22 2011 18:35:11
Server's Module Magic Number: 20051115:25
Server loaded:  APR 1.4.2, APR-Util 1.3.9
Compiled using: APR 1.4.2, APR-Util 1.3.9
Architecture:   64-bit
Server MPM:     Worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/worker"
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=128
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

This tells you the Server MPM being used amongst other things. Make sure the Worker MPM is right for you and your environment before going any further.

#01 - Install Apache MPM Worker Note: Before proceeding with this step, if you have made any changes to the php.ini then I suggest you back it up because the file will be overwritten and you will have to make those changes again. I found this out the hard way. Thankfully I didn’t have many differences from the vanilla file. The new file is located here: /etc/php5/cgi/php.in after the install.

$ sudo apt-get install apache2-mpm-worker php5-cgi
Reading state information... Done
The following packages were automatically installed and are no longer required:
The following packages will be REMOVED apache2-mpm-prefork libapache2-mod-php5
The following NEW packages will be installed apache2-mpm-worker
0 upgraded, 1 newly installed, 2 to remove and 46 not upgraded.
Need to get 0B/259kB of archives. After this operation, 6193kB disk space will be freed.

As you may have noticed by installing apache2-mpm-worker, apache2-mpm-prefork and libapache2-mod-php5 are removed. The PHP5 library is removed because apache2-mpm-worker is not thread safe, scripts in the old library which use the PHP fork(); function would error. php5-cgi was included in the install command for this reason.

#02 - Enable necessary modules

$ a2enmod cgi
$ a2enmod cgid

This does nothing else but creates the proper links to the module .load and .conf files, if you wanted to disable these at any point do so by running a2dismod

#03 - Activate the mod_actions apache modules

$ cd /etc/apache2/mods-enabled
$ sudo ln -sf ../mods-available/actions.load
$ sudo ln -sf ../mods-available/actions.conf

#04 - Modify configuration options to enable the worker module to use the newly installed php5-cgi

$ sudo vi /etc/apache2/mods-available/actions.conf

Add the following to the file:


Action application/x-httpd-php /cgi-bin/php5

#05 - (Optional) Configure your module settings

$ sudo vi /etc/apache2/apache2.conf

Change any of the following to the desired value


StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0

#06 - Verify install/configuration

$ /usr/sbin/apache2ctl -t
Syntax OK

If you got something other than Syntax OK go over the setup and fix any necessary errors.

#07 - Restart Apache to apply changes

$ sudo /etc/init.d/apache2 restart

Happy keyboard mashing,

-Matt

Quake live on Firefox Linux

If you’re having trouble installing Quake Live on Firefox 4 in Linux, don’t fret, there is an easy fix. Download the QuakeLivePlugin_433.xpi. You can download it from the site or here. Once downloaded it open it with Archive Manager and edit the install.rdf. Replace the content in the file with the following:



   
      quakeliveplugin@idsoftware.com
      1.0.433
      
      
            
                  toolkit@mozilla.org
                  1.9
                  2.0.*
            
      
      QuakeLive.com Game Launcher
      Extension required for play on www.quakelive.com
      id Software, Inc.
      true
      
    

Save it and open it with Firefox, everything should go as planned. I can confirm it works and is malware\crapware free.

Happy fraggin.

-Matt

Multi threaded java application development

Most, if not all computers now have multiple cores. With the relatively easy support for multi-threaded development, Java comes well-equipped, ready to use of all that grunt under the hood.

However, the Java Virtual Machine runs in ‘client mode’ (@-client@) by default. Client mode maps all of the java threads onto one single native thread. This has the effect of making the JVM run on only one processor core at any given time.

In order to make the JVM use more than one native thread, you have to supply the -server command line option, in your eclipse.ini, JVM options or run configuration.

Just a short one, if you’re ever developing a multi-threaded application in eclipse on a computer with multiple cores make sure you add -server to your eclipse.ini.

Most applications probably shouldn’t be chewing up the processor, so only use this option if you need too, you may find the fault lies with your application.

For a better explanation check out the Sun “white papers,”:http://java.sun.com/docs/white/langenv/ below is an extracts: <blockquote>The JDK includes two flavors of the VM – a client-side offering, and a VM tuned for server applications. These two solutions share the Java HotSpot runtime environment code base, but use different compilers that are suited to the distinctly unique performance characteristics of clients and servers. These differences include the compilation inlining policy and heap defaults.

Although the Server and the Client VMs are similar, the Server VM has been specially tuned to maximize peak operating speed. It is intended for executing long-running server applications, which need the fastest possible operating speed more than a fast start-up time or smaller runtime memory footprint.

The Client VM compiler serves as an upgrade for both the Classic VM and the just-in-time (JIT) compilers used by previous versions of the JDK. The Client VM offers improved run time performance for applications and applets. The Java HotSpot Client VM has been specially tuned to reduce application start-up time and memory footprint, making it particularly well suited for client environments. In general, the client system is better for GUIs. </blockquote></i>

You really do learn something new every day

-Matt

Domain finally setup

I got around to setting up my custom domain to work with my Heroku app last night. We can now be accessed from www.rukuro.com, rukuro.com or the usual rukuro-blog.heroku.com

-Matt

htaccess pains

I have been lucky enough to play with Apache2 and .htaccess.. if your not getting the desired results you should check the following:

Ports Make sure nothing is stealing your ports Certificates If you’re using certificates make sure the module is enabled and they are being looked up correctly Virtual Hosts Make sure that AllowOverride is not set to None for your file scope (This one really got me, coming from XAMPP which defaults it to All) Modules enabled If using “RewriteEngine” for example, make sure you have enabled it using sudo a2enmod rewrite

If you’re still having problems make sure you check out the apache error log. Default location is /var/log/apache2/error.log it’s usually very helpful. Feel free to comment with any problems you may have had and how you resolved them.

Oh and don’t forget to restart Apache!

-Matt

Ruby readline LoadError

The following error seems to be a common problem with many different fixes on the net. I ran into it today with Rails3/Ruby1.9 installed with RVM.

/usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- readline (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/rvm/gems/ruby-1.9.2-p180/gems/heroku-2.1.1/lib/heroku/command/run.rb:1:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/rvm/gems/ruby-1.9.2-p180/gems/heroku-2.1.1/lib/heroku/command.rb:17:in `block in load'
from /usr/local/rvm/gems/ruby-1.9.2-p180/gems/heroku-2.1.1/lib/heroku/command.rb:16:in `each'
from /usr/local/rvm/gems/ruby-1.9.2-p180/gems/heroku-2.1.1/lib/heroku/command.rb:16:in `load'
from /usr/local/rvm/gems/ruby-1.9.2-p180/gems/heroku-2.1.1/bin/heroku:13:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.2-p180/bin/heroku:19:in `load'

I found a nifty web site that has a long list of the problems, how they were caused and resolved. Check it out here

Although none of them really helped resolve my issue. I resolved mine by running the following commands

$ sudo apt-get install openssl libssl-dev libreadline5-dev zlib1g-dev
$ rvmsudo rvm remove 1.9.2 
$ rvmsudo rvm cleanup all
$ rvmsudo rvm  install 1.9.2 

Note only use rvmsudo if rvm wasn’t installed using the root account. If it was just use rvm instead.

-Matt

Heroku Keyfile

When first running Heroku if you happen to stumble upon this problem have no fear, it’s an easy fix!

Problem

$ heroku keys:add
No ssh public key found in /home/account/.ssh/id_[rd]sa.pub.
You may want to specify the full path to the keyfile.

Solution If it exists find it and run the command pointing to it or create one by running the following command

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/account/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/account/.ssh/id_rsa.
Your public key has been saved in /home/account/.ssh/id_rsa.pub.
The key fingerprint is:

Also if you’re ever having an issue cloning an existing Heroku app make sure you use the -o argument. Without it I was getting the usual and not very helpful Permission denied (publickey) exception. So yeah, when pulling down an existing app from Heroku run

git clone -o heroku git@heroku.com:appname.git

instead of

git clone heroku git@heroku.com:appname.git

The -o flag sets the remote name to heroku instead of origin, to make it consistent with the remote that the heroku CLI automatically creates.

-Matt

Apache port issues

If you ever get errors restarting apache or an SSL exception like the one below ssl_error_rx_record_too_long It’s most likely because something is using the ports that Apache is trying to use. I know this is most likely not new to most people but if you are having issues this could be it. I have found that Skype is a repeat offender using ports 80 and 443 by default.

-Matt

Heroku command grief

After installing Heroku on Ubuntu following I kept getting the following error when trying to use it on the command line: WARNING: Unable to verify SSL certificate for api.heroku.com To disable SSL verification, run with HEROKU_SSL_VERIFY=disable

I tried to Google it but found no results, I didn’t understand why it was occurring in the first place. To get around it though simply do as it says and type “HEROKU_SSL_VERIFY=disable” before you execute your command. For example $ HEROKU_SSL_VERIFY=disable heroku create

This works for executing that single line only. Either run $ export HEROKU_SSL_VERIFY=disable to save you adding it before each command for that session or just add it to the bottom your bashrc file to not have to worry about it again. $ vim ~/.bashrc # Add export HEROKU_SSL_VERIFY=disable to the bottom of the file

-Matt

Resize pictures

Thought I would take a moment to share a handy tool.

Microsoft has an awesome little utility (yeah I know) called Image Resizer it was originally a Powertoy designed for Windows XP but it’s been ported to Windows 7 and Vista (32 and 64bit supported).

Find it here .

Basically after the installation “Resize Pictures” will be added to your context menu. To resize pictures simply select a bunch right click > resize and select your size.

Screens Context: Photobucket Advanced: Photobucket

While we’re on the subject of handy tools, check out TeraCopy, TextPad and foobar2000. Great apps I have installed on pretty much every machine I use.

-Matt

Ding! Leveling up

Had a riot this weekend. Squandered most of it in front of my laptop playing with Linux, Rails, Ruby, Heroku and GitHub while waiting for my domain name changes to take effect.

I have decided to start a blog for all my technological encounters (battles). I seem to solve a new problem every day, I believe it’s worth while documenting them, hopefully helping someone else out in the process.

Big thanks to Chris Aitchison (http://www.chrisaitchison.com/) for putting me on this new and exciting path. This blog was built off Enki’s blog (http://www.enkiblog.com/) check it out if you feel like forking up your own! It’s neat and easy to use.

Back to tinkering!

-Matt