Datatypes in Javascript – setting the record straight


It’s a question that often comes up and there are many places on the internet that give an inaccurate answer, so here’s the real-deal.

Javascript has three primitive datatypes:

  • number
  • string
  • boolean

There are another two special datatypes:

  • null
  • undefined

The others are variations of object:

  • object
  • function
  • array

See here for further reading.

Detecting an invalidly parsed date in Javascript


When parsing a date in Javascript, it is important to ensure that the resultant object has been parsed correctly. This is made slightly tricky as the resultant object will always be a Date object if created with new Date().

The problem is compounded by differences between IE and other browsers. IE will return a NaN whereas other browsers will report "Invalid Date".

Use the following code to be sure:

var d = new Date("Malformed date")
if (d != "NaN" && d != "Invalid Date") {
  // date is good
} else {
  // date is bad
}

Does anyone else have any good ways of doing this detection?

Reading Geek Night


An open community event for techy-types is getting underway in Reading.  These events are for people to get together in an informal setting, share knowledge and have a couple of beers!

The first of these monthly events is happening next Tuesday on the 10th November.  Add http://readinggeeknight.com/ to your bookmarks and check it out regularly for updates.  You can also follow @rdggeek on twitter or the #rdggeek hashtag.

Talks lined up for the first event are:

Who do you think you are? Ben NunneyThe internet is everywhere, and all of us in the tech community are connected in some way, shape or form. But how powerful is your digital identity, and what does it actu- ally matter to you? The talk will be a brief look at what your digital identity is, was, and can be – who are you, in the eyes of the internet? Does your boss really care what you had for lunch?

A practical introduction to Ruby on Rails Chris TingleyThis live code demonstration will take you from your first line of Ruby code, through some fundamental features of Rails with pit stops in MVC, CRUD, REST, automated testing and meta-programming, crossing the finish line with a functional web app! Hold on to your hats, all this WILL happen in 15 minutes.

What Is Windows Azure? Dom GreenWhat is Windows Azure? How will cloud computing change how we develop applications, manage our IT infrastructure, or even set up an online business? This whirl-wind talk will introduce Windows Azure, Microsoftʼs cloud computing platform.

Coding for kids Jim Anning – Coding is not routinely taught in UK schools until 6th form level. How can we encourage a new generation of kids to code? A quick introduction to MIT’s Scratch programming environment.

Installing modporter for Rails on Ubuntu Hardy Heron


It’s not an uncommon scenario for Apache to not play nicely with Rails when dealing with multipart forms and large uploaded files. There are a few solutions out there, but the best we have seen is modporter from the guys at ActionRails.

So, you’ll need a few things to beef up Apache; if you haven’t got them already:

sudo apt-get install libapache2-mod-apreq2
sudo apt-get install libapreq2-dev
sudo apt-get install apache2-threaded-dev

Now fetch the latest ModPorter from github from here. Once you have downloaded and unpacked this, edit the Rakefile and change the apxs variable to apxs2, then type:

cd modporter-directory
rake

And, you’ll probably want the modporter plugin for Rails to give you a nice consistent interface for uploaded files rather than differentiate String IOs and UploadedFile objects.

If you have git:

script/plugin install git://github.com/actionrails/modporter-plugin.git

Otherwise, download the tar from here, unpack and stick in your vendor/plugins directory.

Now configure Apache to use load some additional modules, edit your apache.conf file to include (your path to modules may be different):

LoadModule apreq_module modules/mod_apreq2.so
LoadModule porter_module modules/mod_porter.so

Tracking down a stolen laptop


This is a bit off-topic, but worthwhile sharing.

I recently, rather unfortunately had my laptop stolen and now I have a replacement; I remember hearing about software that could use the built in iSight to take photos of the thieves and mail them back to me.

After looking on the net and finding some rather good, but pay-ware applications, I came across the Prey Project offering an open source free alternative. I went about installing the software and after checking that it worked went about tweaking it to work with how I have my laptop set up.

I use file vault (thank God it was switched on the laptop that was stolen) and have a password on wake so my personal files are secure. However, even with crontab running Prey once a minute, I figured that if the thief couldn’t log into my laptop, it was a bit pointless. So I have used two tactics that will hopefully result in my getting some pictures if the unfortunate were to happen again:

  1. Enable logging in of the Guest account. This means if the thief reboots the laptop (to try and log in as a different user rather than get the password screen from wake), they can log into a sandboxed account with Prey happily snapshotting away.
  2. Put Prey in the crontab that runs for the login screens:

in /private/etc/crontab (this may not exist):

*/1 * * * * cd /usr/share/prey; ./prey.sh

Of course, there are no guarantees, but it’s better than nothing.

10,000 Hits!

Just a quick note to say we have just reached 10,000 hits to the Workbooks Technical Blog. Thanks for reading!

./java: No such file or directory (on Linux)


… and when you look, it exists, it has the correct permissions and everything. Do a long-listing and it’s readable and executable by everyone, and all the directories up the tree also are.  It just doesn’t start!  I spent a while looking for this and didn’t find it on google so hopefully this helps someone.

The clues:

$ file java
java: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped


$ uname -a

reports a 64-bit OS.

The problem is you’re running a 64-bit OS with insufficient 32-bit shared library support to run a 32-bit binary.

The solution:

$ sudo aptitude install ia32-libs

(Courtesy of http://www.debian-administration.org/articles/534)


PS: Yes, I know I could get a 64-bit java interpreter….

Ruby is beautiful – longest word length


Sometimes it makes me smile how things can be implemented in such a simple and beautiful way in Ruby.

We needed a method to return the length of the longest word in a string. So we opened up the String class and gave it a new toy to play with:

class String
  # Returns the length of the longest word in the string
  def longest_word_length
    words = self.split(' ')
    words.any? ? words.max{ |a,b| a.length <=> b.length }.length : 0
  end
end

Of course, an RSpec example will keep our testing Yang intact!

describe String do
  it "can determine the longest word in a string" do
    "".longest_word_length.should eql(0)
    "hello".longest_word_length.should eql(5)
    "this is the longest word".longest_word_length.should eql(7)
    "two words".longest_word_length.should eql(5)
    "same size".longest_word_length.should eql(4)
  end
end

Are there any little methods you have written that make you smile at their simplicity?

Comparison <=> operator on string attributes of ActiveRecord objects


Here’s a little gotcha for you which caught me out by surprise, obvious when you think about it, but irritating if you miss it: the ruby String comparison operator does case-sensitive comparisons.

# Good:
['C', 'b', 'A', 'D'].sort #=> ['A', 'b', 'C', 'D']

# Bad (what <=> gives you out the box):
['C', 'b', 'A', 'D'].sort #=> ['A', 'C', 'D', 'b']

For a nice efficient implementation of a AR model comparitor use the String#casecmp method for a case-insensitive comparison:

class MyObject
  def <=>(other)
    self.my_attribute.casecmp(other.my_attribute)
  end
end

Overriding fundamental ActiveRecord methods (like destroy) whilst preserving callbacks


OK, so you’ve decided that it’s a good idea for you to override one of the fundamental methods on one of your ActiveRecord models. But in doing so, you have discovered that all of a sudden your callbacks have stopped working.

The reason for this is when your class is loaded, ActiveRecord::Callbacks have already hooked in the callback methods around the relevant methods using alias_method_chain meaning that the once named destroy method is now called destroy_without_callbacks.

So, to actually override the destroy method, use the following code:

def destroy_without_callbacks
  unless new_record?
    # do your code here
  end
  freeze
end

Note that the implementations will differ for other methods e.g. create, so you should check the ActiveRecord base class to see what should be in there.