What I would like in “borrowed” into Ruby

C Style comments

  /* comments */
  // and those while we're here.

C style multiline curly braces (this actually works, but didn’t think it would due to following conventions, but in some case could make code easier to read)

  {
     // code here ....
  }

Modula 3 style named end blocks (possible activated via a strict mode)

  def method_name
    ...
  end method_name.

Pascal style “with” blocks

  with the_object do
    push << "hello"
    splice something
  end

can you think of anything you’d like borrow from another language into your favourite language?

Whenever you come across a site that emails you your password

You should email the administrator with a message to that effect.

I have noticed that when as user tells the site that they have forgotten their password, that the site will send the password to their email address.

This is not good practice, when your data gets compromised (one day it will) every password will be there in plain text in the database.

This is bad as come users may use that email address and password combination on other sites.

You should look into SHA-2 hashing of passwords and salting.

And rather than send the password in an email you should send a reset key.

I know that the current specification for the site may have come from someone not as security conscious who thinks that sending a password is more user friendly (although it seems it) but it is a security flaw.

The reason I want to work with rails

I was initially an admirer of Ruby on Rails quite early on (2005), and did a few examples but carried on working on classic asp then asp.net whilst still following Rails. The main reason for not having used Rails earlier on is that the jobs market was full of asp.net and php shops, and I liked Ruby much more than the other languages but had given myself enough time to use it.

But now I have used Ruby On Rails for fully featured applications and I am looking to work under a team which can help me develop further in my Ruby On Rails career, rather than the lone programmer role; who has to justify not only Myself but Ruby On Rails.

Using form_dropdown in codeigniter to populate from the database

in the controller send this:

   $data["fruits"] = $this->db->get('fruits')->result();

Now you could go and loop through each fruit and create a select form input tag and create options with a selected option like this:

    echo "<td><select id='". $fruitbasket->id ."'>";
   
    echo "<option value=''></option>";
   
    foreach($fruits $f){
        if($f->id == $fruitbasket->moldiest){
            $selected = " selected=selected ";
        } else {
            $selected = "";
        }
       
        echo "<option $selected value=" . $f->id . ">" . $f->name . "</option>";
    }
   
    echo "</select>";

or you could use the form_dropdown helper:

    $fruit_options = array();
    foreach($fruits as $fruit){
        $fruit_options[$fruit->id]=$fruit->name;
    }
    echo "<td>" . form_dropdown('', $fruit_options, $fruitbasket->moldiestfruit,$fruitbasket->id) . "</td>";

The tiny bit of overhead is that codeigniter puts the result in an array of objects rather than an array of arrays which form_dropdown asks for, so the tiny loop is required to create the options variable.

Splitting a csv file using bash in linux

This is a typo update on a post i’ve seen on this blog post http://www.geekology.co.za/blog/2009/02/bash-script-to-split-single-csv-file-into-multiple-files-with-headers/

What this will do is split a csv file into chucks of 300 lines but also prepend the original header from the csv file to the chunks that have been cut.

#!/bin/bash

# check if an input filename was passed as a command line argument:

if [ ! $# == 1 ]; then  echo "Please specify the name of a file to split!"  exit

fi

# create a directory to store the output:
mkdir output

# create a temporary file containing the header without
# the content:
head -n 1 $1 &gt; header.csv

# create a temporary file containing the content without

# the header:
tail -n +2 $1 &gt; content.csv

# split the content file into multiple files of 5 lines each:
split -l 300 content.csv output/data_

# loop through the new split files, adding the header# and a '.csv' extension:
for f in output/*; do cat header.csv $f &gt; $f.csv; rm $f; done;

# remove the temporary files:
rm header.csv
rm content.csv

You can change it to any number you want, but this is the ideal number of records for importing into a sugarcrm setupon a fasthosts site. If you use any other hosting the memory on sugarcrm may max out at a different level,so just tweak that number a little.

for a non-sugarcrm solution, for example loading into excel you could have a value of 50,000 to beat the 64,000~ barrier.

by the way anyone who want to analyse a 64k line excel file and make business critical decisions based on that is an idiot.

Finally My work is here to see


Back in September I was hired by JobsGroup.net to help out with the web design (yes, photoshop) and some backend coding of their new jobs portal system. Finally after some backend problems and two months later the fruits of my labour are for all to show.

I’m a little perplexed tho as to some of the bugs that I had noticed on the last day of my contract still being there two months after, but in general I am quite proud of my work.

Most notably what I am quite proud of in the backend would be finding out how to use RaiseBubbleEvent, delegates, and asp.net events.

I have since made leaps and bounds with services and ajax, my preference is jQuery and sometimes little “homebrew” mashups work a treat to expand ones knowledge.

Now back to just site, you will see things such as the jobsbasket, mysavedsearch, vacancyview screens, mailme and mail a friend popups. I have contributed to those, not entirely sure whether they are still mine but they were in good condition when i left them :)

What I am most happy about is being able to say that I have had full development lifecycle experience.

And what I am not to happy about is the current economic climate which made my stay only last the duration of my contract. JobGroup.net is a great company to work with/for/etc..

Get ready to drop SVN, here’s GIT!! Well for tortoise users anyways!

http://code.google.com/p/tortoisegit/

TortoiseGIT is out! This is briliant news, as I am a GIT user when it comes to trying out the latest opensource applications.

GIT is superior to SVN, as it is a distributed source control system as opposed to a centralized one. But plenty of windows users stick to SVN because of the usefulness of TortoiseSVN.

When I create projects I usually use the command prompt to do the first update to a folder. Then I can just right click and use TortoiseSVN.

This is briliant news. I think I will try and commit my next project to GitHub, one advantage of GitHub is the ability to have private use repositories for commercial use. I might even contribute to some now.

I was seriously considering switching to Ubuntu for developing (although having to drop windows development in the process) just to have a better use of GIT and SVN through the CLI (command line interface)

In Linux the equivalent of Tortoise SVN is a nautlius plugin

http://marius.scurtescu.com/2005/08/24/nautilus_scripts_for_subversion

I have use GIT in the CLI and it’s pretty simple though.

Displaying Isometric tiles using Javascript.

What do you think?

well the little peice of magic code that does that is:

var isox = Math.round((this.x - this.y) * h * 0.5) + xoffset;
var isoy = Math.round((this.x + this.y) * w * 0.25) - yoffset;

The * h * 0.5 means that the isometric tile is half as height as the actual image file.

and the * w * 0.25 means the tiles must join each other by a quarter of the image size.

This formula isn’t a stagered coordinate system, its just bog standard x and y coordinates. pretty simple really.

For an exmaple of it in real world code (the screenshot) check here for update of the project http://code.google.com/p/ajaxmmo

How to read a grayscale image in Python using pypng

what we start with is to include png.py like this:

[code lang="python"]import png.py[/code]

with png.py we wil now open a png reader from a specified path and then just interate through each pixel, with grayscale you just read one color value, we can ignore the other values such as green and blue as all color should be at the same scale. I have notice slight fluctuations in the values, but that just depends on how your image editor saves the file.

here is my code:

[code lang="python"]
reader = png.Reader(os.path.join(os.path.dirname(__file__), 'static', 'earth.png') ) # streams are also accepted
    w, h, pixels, metadata = reader.read()
    pixel_byte_width =  4 if metadata['has_alpha'] else 3

    fov =  2# fov is how many tiles a unit can see around it
    xleft = max(unit.x - fov, 0)
    xright = min(unit.x + fov + 1, w)
    ytop = max(unit.y - fov, 0)
    ybottom = min(unit.y + fov + 1, h)

    for x in range(xleft, xright):
        for y in range(ytop, ybottom):

            pixel_position = (y * w) + x

            pixel = pixels[
              pixel_position * pixel_byte_width :
              (pixel_position + 1) * pixel_byte_width]

            alt = pixel[1]

[/code]