Learning Javascript with Crockford and Spidermonkey

JavaScript No Comments »

As JavaScript is the language I’m planning to learn more in-depth this year, primarily to approach the latest map-reduce API’s of the latests NoSQL databases (CouchDB, riak etc.), I’ve recently made good progress thanks to two invaluable resources:

Crockford Videos

First, Douglas Crockford’s lecture videos at the YUI theater. He has created three separate series, The JavaScript Programming language, Advanced JavaScript. and more recently, Crockford on JavaScript, all available on the same page in the YUI theater. The third episode (Act III) of Crockford on Javascript gives you a tour de force of functional programming and is probably the most valuable of all the videos for advanced programmers.

He also states in his first lecture that there are no good books on JavaScript, except perhaps a single one, JavaScript: The Definitive Guide By David Flannagan. You may also want to check out Crockford’s own books which I haven’t had the chance to read yet.

Finally, always use Crockford’s jslint to check your javascript code quality. It’s a great teaching tool in and of itself and will help you find all those missing semi-colons and curly braces.

Spidermonkey Command Line

Interactively checking code on the command line is standard practice in the ruby and python universes. Now you can easily have the same code checker at your fingertips with JavaScript. In Ubuntu (8.04 and higher) simply

sudo apt-get install spidermonkey-bin

, and then type

> js

to get the beautiful prompt:

js>

Here are some tips for working with the spidermonkey command line. And, to use jslint from the command line, check out this post.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Netbeans Regular Expressions Find and Replace

Netbeans, Programming, Quanta Plus No Comments »

Powerful find and replace with regular expressions should be the hallmark of every IDE worth its mettle. MS Visual Studio does it so-so, but lo and behold, Netbeans (at least in version 6.1) is even worse.

So I wanted to put foo at the beginning of every line in a text, which calls for a regex like:

^.*

and replace like:

foo $0

However, Netbeans only ever replaces the first match in a text, over and over again. It also hangs up easily in the process.

As with many a Netbeans quirk, it’s Quanta Plus to the rescue again:

Quanta_plus_utf-10

Works like a charm.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Populate an ActiveRecord Model with Images Using attachment_fu

Programming, Ruby on Rails No Comments »

Is there anything more painful than manually populating your model with sample data? The thought alone makes most developers’ skin scrawl because we write code to do the work for us and abhor manually entering fugacious data.

As always, first things first, watch the relevant Railscast, which will introduce you to populator and faker, both great tools you will soon find indispensable. And here’s a little rake task to populate your model with images, useful to see how it looks with product thumbnails or the like. This is just sample code which you should edit to suit your needs. And attachment_fu has to be configured and working. The file, which could be named attachment_populate.rb belongs in the lib/tasks/ directory:

namespace :attachment do
  desc "Add an image to all MyModel items for visual checking"
  task :add_image_to_all_mymodels  => :environment do
    require 'action_controller'
    require 'action_controller/test_process.rb'
    path = "#{RAILS_ROOT}/public/images/samples/sample.jpg"
    mimetype = "image/jpeg"
    MyModel.find(:all).each do |mymodel|
      @attachment = Attachment.new(:uploaded_data => ActionController::TestUploadedFile.new(path, mimetype))
      @attachment.mymodel_id = mymodel.id
      @attachment.save
    end
  end
end

Call with

rake attachment:add_image_to_all_mymodels

The pertinent documentation is in the #{RAILS_ROOT}/vendor/plugins/attachment_fu/README under “attachment_fu scripting”:

# required to use ActionController::TestUploadedFile
require ‘action_controller’
require ‘action_controller/test_process.rb’

path = “./public/images/x.jpg”

# mimetype is a string like “image/jpeg”. One way to get the mimetype for a given file on a UNIX system
# mimetype = `file -ib #{path}`.gsub(/\n/,”")

mimetype = “image/jpeg”

# This will “upload” the file at path and create the new model.
@attachable = AttachmentMetadataModel.new(:uploaded_data => ActionController::TestUploadedFile.new(path, mimetype))
@attachable.save

 And where do you get more info about creating rake tasks? Railscasts of course, who needs books?

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Change the Netbeans Windows Layout

Netbeans, Programming No Comments »

In Netbeans there currently (Version 6.1) is no built-in option to save a windows layout, you can only do a “Reset Windows”  (in the Window menu) to get back to the factory layout.
I’ve been looking for a solution to get a *cough* Visual Studio *cough* like layout (all navigation on the right of the code window) and found a great plugin called Perspective (click this link twice as for some reason on the first click it will give you the plugin home page) that lets you save a layout and switch easily between layouts (er, perpectives). You can’t download it using the Netbeans plugin installer as it’s not signed, so you’ll have to download and install from disk: 

Netbeans_window_layout_perspective_plugin

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Generating CHM Documentation for RSpec on Rails

Programming, Ruby on Rails No Comments »

RSpec is the king of the BDD hill when it comes to Rails, and the learning curve is quite steep. For Windows developers hooked on CHM documentation, it’s very easy to generate RSpec CHM files by running the command

rdoc -f chm -o chm_folder rspec*

in the

/vendor/plugins

directory of your RoR project where you have installed the RSpec and RSpec on Rails plugins using

ruby script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec
ruby script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails

as described on the RSpec home page.

The resulting CHM is not the prettiest but it’s fully searchable and easier to use than sifting through the online rdocs in your browser.

Update December 5, 2008: RSpec is under heavy development, and installation instructions change frequently, so you should always refer to the rspec home page for details.

RSpec and RSpec for Rails are now gems only so to generate the chm file cd to your gems directory (usually C:\ruby\lib\ruby\gems\1.8\gems), which you can determine by running

gem env

at the command prompt, and then run

rdoc -f chm -o chm_folder rspec* -x helper_spec.rb

helper_rspec.rb has to be excluded as it causes the rdoc generation to hang. Here’s the updated resulting chm file for rspec, raw and unedited.

P.S.: This only works on Windows as it depends on HTML Help Workshop but the chm file can be viewed with other chm viewers. I recommend xCHM for Linux.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in