<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1073</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1073" rel="self"/>
  <title>Nathan</title>
  <updated>Mon Sep 05 07:08:25 -0700 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1801</id>
    <published>2011-09-05T07:08:25-07:00</published>
    <updated>2011-09-05T10:03:49-07:00</updated>
    <title>[Ruby] Find valid URLs</title>
    <content type="html">&lt;p&gt;Some selected URLs exist which are identical apart from a number. I want to find all the valid numbers. Here's my attempt.&lt;/p&gt;

&lt;pre&gt;require 'open-uri'
require 'net/http'

def remote_file_exists?(url)
  url = URI.parse(url)
  Net::HTTP.start(url.host, url.port) do |http|
    return http.head(url.request_uri).code == &amp;quot;200&amp;quot;
  end
end

search_url_start = 'http://example.com/'
search_url_end = '.jpg'
(1..9999).each do |i|
  puts i if remote_file_exists?(search_url_start + i + search_url_end)
end&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1801-find-valid-urls" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1497</id>
    <published>2010-11-18T09:49:07-08:00</published>
    <updated>2011-02-04T14:04:00-08:00</updated>
    <title>[JavaScript] jQuery script: too many ifs and elses</title>
    <content type="html">&lt;p&gt;This is part of a jQuery plugin offering a stripped-down validation of form fields. It checks 3 things... that it isn't empty if the notEmpty var is true; that it matches a regExp if one is provided, and that it exceed the minimum length if one is provided. It marks erroneous methods as such with the invalidClass var.&lt;/p&gt;

&lt;p&gt;After it has done all its checks, it then checks again if the field is valid, and if it is it runs valueProcess() on the value.&lt;/p&gt;

&lt;p&gt;I am aware of a potential bug with things being marked as invalid if they are left blank when notEmpty is false but other validations exist. That hasn't caused us a problem yet, and I'd rather get the code trimmed down a bit before I sort that out.&lt;/p&gt;

&lt;p&gt;Edit: forgot to mention, it also adds/clears an error message after the field.&lt;/p&gt;

&lt;pre&gt;var value = $.trim($(this).val());

if ($(this).hasClass(invalidClass))
  if (
      (!notEmpty || value != '')
      &amp;amp;&amp;amp; (!regExpMatch || value.match(regExpMatch)) 
      &amp;amp;&amp;amp; (!minLength || value.length &amp;gt;= minLength)
     )
    $(this).removeClass(invalidClass);
else
  if (
      (notEmpty &amp;amp;&amp;amp; value=='')
      || (regExpMatch &amp;amp;&amp;amp; !value.match(regExpMatch))
      || (minLength &amp;amp;&amp;amp; value.length &amp;lt; minLength)
     )
    $(this).addClass(invalidClass);
if (!$(this).hasClass(invalidClass)) {
  $(this).next('.error-message').remove();
  value = valueProcess(value);
}
else if ($(this).next('.error-message').size()==0)
  $(this).after(
      '&amp;lt;span class=&amp;quot;' + errorMessageClass + '&amp;quot;&amp;gt;'
      + message
      + '&amp;lt;/span&amp;gt;'
  );
$(this).val(value);&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1497-jquery-script-too-many-ifs-and-elses" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor493822</id>
    <published>2010-04-13T13:24:08-07:00</published>
    <title>[PHP] On horrific html</title>
    <content type="html">&lt;p&gt;Are you asking us to fix the HTML? That's not really what the site's for. Having said that, at first glance you need a &amp;lt;/iframe&amp;gt; on line 32 and a &amp;lt;/tr&amp;gt; on line 45, &amp;lt;head/&amp;gt; should be &amp;lt;/head&amp;gt; and I don't know where all of those &amp;quot;&amp;lt;a&amp;gt;&amp;quot;s and the &amp;lt;ul&amp;gt; have come from, but they shouldn't be there. I assume there are some I've missed too.&lt;/p&gt;

&lt;p&gt;I suggest you take a look at an HTML tutorial. There's a reasonably good one here: &lt;a href="http://w3schools.com/html/default.asp" target="_blank"&gt;http://w3schools.com/html/default.asp&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1257-horrific-html/refactors/493822" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor492673</id>
    <published>2010-04-07T08:34:18-07:00</published>
    <title>[Ruby] On bluetooth Device class</title>
    <content type="html">&lt;p&gt;I like what you've done with both methods. The map method is one that I should remember for future, but what you did with the new method and the class level array is just plain clever.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1252-bluetooth-device-class/refactors/492673" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1252</id>
    <published>2010-04-06T09:50:19-07:00</published>
    <updated>2010-04-07T08:34:20-07:00</updated>
    <title>[Ruby] bluetooth Device class</title>
    <content type="html">&lt;p&gt;I've put together a quick ruby bluetooth class. All it does at the moment is search for devices and send files to any selected device (which is all I need). It uses system commands, which isn't ideal, but I can't really see a better way. The main bits which need refactoring are the bits where I mess about with the new method... not sure what can be done here, and the find_new class method, which scans for devices and returns an array of Device objects (I'm terrible with regular expressions).&lt;/p&gt;

&lt;pre&gt;class Device
  @@all = Array.new
  
  attr_reader :mac, :name
  
  # need this funny bit to alias class methods
  class &amp;lt;&amp;lt; self
    alias_method :old_new, :new
  end

  # if the device is already listed in all_devices, return the
  # existing object instead of creating a new one
  def self.new *args, &amp;amp;b
    @@all.each do |device|
      return device if args.first == device.mac
    end
    this = self.old_new *args, &amp;amp;b
    @@all &amp;lt;&amp;lt; this
    this 
  end

  def initialize mac, name
    @mac = mac
    @name = name
    @sent = {}
  end

  def sent? file
    @sent[file]
  end
  
  def send! file, timeout=8
    result = `ussp-push --timeo #{timeout} #{@mac}@ #{file} #{file.name}`
    @sent[file] = ($?.to_i == 0 &amp;amp;&amp;amp; result.scan(/Connection established/))
  end

  def self.find_new
    device_objects = Array.new
    devices_string = `hcitool scan --flush`
    available_devices = devices_string.scan(/[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]\t[^\n]*/i)
    available_devices.each do |detected_device|
      mac_addr = detected_device.scan(/[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]/i).first
      dev_name = detected_device.scan(/\t[^\n]*/i).first.gsub(/\t/,'')
      new_device = Device.new mac_addr, dev_name
      device_objects &amp;lt;&amp;lt; new_device
    end
    device_objects
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1252-bluetooth-device-class" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor490396</id>
    <published>2010-03-31T13:59:39-07:00</published>
    <title>[JavaScript] On Escape HTML</title>
    <content type="html">&lt;p&gt;D'oh, just realised you can do it in one line with JQuery, which I am using in my project.&lt;/p&gt;

&lt;pre&gt;$('#my-element').text($('#my-element').html());&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1245-escape-html/refactors/490396" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1245</id>
    <published>2010-03-31T13:48:52-07:00</published>
    <updated>2010-07-03T19:22:58-07:00</updated>
    <title>[JavaScript] Escape HTML</title>
    <content type="html">&lt;p&gt;All HTML special characters can have their numeric values used instead of their normal symbols. So &amp;amp;gt; can also be represented by &amp;amp;#62;. Javascript's encodeURI() converts characters into these numbers, but uses the HEX versions. I've made a simple function using this idea to escape HTML. I tried doing it using back references, but couldn't get it working.&lt;/p&gt;

&lt;pre&gt;function encodeHTML(html)
{
  encoded = encodeURI(html);
  encodedChars = encoded.match(/%([ABCDE0-9]+)/g);
  for (encodedCharNumber in encodedChars)
  {
    encodedChar = encodedChars[encodedCharNumber];
    htmlEncodedChar = '&amp;amp;#' + parseInt(encodedChar.replace('%',''),16) + ';';
    encoded = encoded.replace(encodedChar,htmlEncodedChar);
  }
  return encoded;
}&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1245-escape-html" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor484576</id>
    <published>2010-03-19T13:20:04-07:00</published>
    <title>[Ruby] On Create database records</title>
    <content type="html">&lt;p&gt;Refactoring isn't really meant to be about speed. If you're concerned about speed, Ruby on Rails probably isn't the best choice of technology to use. Refactoring is about simplicity, readability, scalability, etc. These attributes often come at the expense of execution speed, but the belief is that lower development time and cost make up for the additional cost of better hardware.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1232-create-database-records/refactors/484576" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor471853</id>
    <published>2010-03-08T08:51:43-08:00</published>
    <title>[JavaScript] On Rotate co-ordinates</title>
    <content type="html">&lt;p&gt;Great. It passes my unit tests and it looks nice and simple, so I'm happy.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1205-rotate-co-ordinates/refactors/471853" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1205</id>
    <published>2010-03-05T15:49:42-08:00</published>
    <updated>2010-06-24T12:09:34-07:00</updated>
    <title>[JavaScript] Rotate co-ordinates</title>
    <content type="html">&lt;p&gt;A nice and fun one here. I want to be able to rotate some co-ordinates either clockwise or anticlockwise around the 0,0 point. The temporary methods are a bit of an irritation, and it seems unnecessary to do the whole thing twice.&lt;/p&gt;

&lt;pre&gt;this.rotate = function (antiClockwise)
{
  if (antiClockwise)
  {
    x = -1*this.y;
    y = this.x;
    this.x = x;
    this.y = y;
  }
  else
  {
    x = this.y;
    y = -1*this.x;
    this.x = x;
    this.y = y;
  }
}
&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1205-rotate-co-ordinates" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor467020</id>
    <published>2010-03-03T06:54:37-08:00</published>
    <title>[JavaScript] On Search a string with wildcards</title>
    <content type="html">&lt;p&gt;Thanks for that DPM. I had considered using regular expressions,  but it's for matching user inputed match strings in a browser extension, so the data will be a string, and it needs to be much simpler so the user doesn't have to escape special characters. As for the error, yes, I'm getting that too... looks like I was a bit hasty in submitting, because I didn't see it before. I'll try and work out what's wrong.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1201-search-a-string-with-wildcards/refactors/467020" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1201</id>
    <published>2010-03-03T00:48:28-08:00</published>
    <updated>2010-03-09T21:05:14-08:00</updated>
    <title>[JavaScript] Search a string with wildcards</title>
    <content type="html">&lt;p&gt;I want to have a string which is can be searched by another string which contains * wildcards. I was having trouble, so I made a simple test framework to help me get it working. I've included the tests so that any refactorings can be checked.&lt;/p&gt;

&lt;p&gt;Edit: I've found the problems that meant it didn't run and fixed them so it should be fine now. I've also added the extra test cases suggested below.&lt;/p&gt;

&lt;pre&gt;## function to refactor

function matches(matchString,list)
{
  goodSoFar = false;
  parts = matchString.split('*');
  for (i = 0; i &amp;lt; list.length; i++)
  {
    for (j = 0; j &amp;lt; parts.length; j++)
    {
      if (list[i].indexOf(parts[j]) &amp;gt; -1)
      {
        if (parts[j] != '')
        {
          list[i] = list[i].split(parts[j]);
          list[i][0] = '';
          list[i] = list[i].join();
        }
        goodSoFar = true;
      }
      else
      {
        goodSoFar = false;
        break;
      }
    }
    if (goodSoFar)
      return true;
  }
  return false;
}

## tests - each if is a new test

function generateList()
{
	list = new Array(&amp;quot;The quick brown fox jumps over the lazy dog&amp;quot;,&amp;quot;All work and no play makes Jack a dull boy&amp;quot;,&amp;quot;qwerty&amp;quot;);
	return list;
}

fails = new Array();

testList = generateList();
if (!matches(testList[0],testList))
  fails.push(&amp;quot;Simple string match - should be true&amp;quot;);

testList = generateList();
if (matches(&amp;quot;Non-matching string&amp;quot;,testList))
  fails.push(&amp;quot;Simple non-matching string - should be false&amp;quot;);

testList = generateList();
if (!matches('*',testList))
  fails.push(&amp;quot;Asterisk - should be true&amp;quot;);

testList = generateList();
if (!matches('All work*',testList))
  fails.push(&amp;quot;Part string then asterisk - should be true&amp;quot;);

testList = generateList();
if (!matches('The quick*',testList))
  fails.push(&amp;quot;The quick then asterisk - should be true&amp;quot;);

testList = generateList();
if (matches('no match*',testList))
  fails.push(&amp;quot;Non-matching with * - should be false&amp;quot;);

testList = generateList();
if (!matches(testList[1],testList))
  fails.push(&amp;quot;Simple string match 2 - should be true&amp;quot;);

testList = generateList();
if (!matches('*lazy dog',testList))
  fails.push(&amp;quot;Asterisk then part string - should be true&amp;quot;);

testList = generateList();
if (matches('*no match',testList))
  fails.push(&amp;quot;Asterisk then non-matching string - should be false&amp;quot;);

testList = generateList();
if (!matches('The quick*lazy dog',testList))
  fails.push(&amp;quot;Match with asterisk in middle - should be true&amp;quot;);

testList = generateList();
if (matches('The quick*no match',testList))
  fails.push(&amp;quot;Part match with asterisk in middle - should be false&amp;quot;);

testList = generateList();
if (!matches('The quick*jumps over*lazy dog',testList))
  fails.push(&amp;quot;Match with two asterisks in middle - should be true&amp;quot;);

testList = generateList();
if (matches('The quick*wrong*lazy dog',testList))
  fails.push(&amp;quot;Incorrect match with two asterisks in middle - should be false&amp;quot;);

testList = generateList();
if (!matches('*The quick brown fox jumps over the lazy dog',testList))
  fails.push(&amp;quot;Asterisk matching '' at the start - should be true&amp;quot;);

testList = generateList();
if (!matches('The quick brown fox jumps over the lazy dog*',testList))
  fails.push(&amp;quot;Asterisk matching '' at the end - should be true&amp;quot;);

testList = generateList();
if (matches('The quick*over*jumps*lazy dog',testList))
  fails.push(&amp;quot;Seperate parts match but incorrect order - should be false&amp;quot;);

## render results

if (fails.length == 0)
  document.write('&amp;lt;div style=&amp;quot;width:100%;background-color:green;color:white;text-align:center&amp;quot;&amp;gt;All tests pass&amp;lt;/div&amp;gt;&amp;lt;br /&amp;gt;&amp;amp;nbsp;');
else
  document.write('&amp;lt;div style=&amp;quot;width:100%;background-color:red;color:white;text-align:center&amp;quot;&amp;gt;The following tests have failed:&amp;lt;br /&amp;gt; '
      + fails.join('&amp;lt;br /&amp;gt;') + '&amp;lt;/div&amp;gt;');&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1201-search-a-string-with-wildcards" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1191</id>
    <published>2010-02-26T23:46:41-08:00</published>
    <updated>2010-03-02T23:49:08-08:00</updated>
    <title>[JavaScript] Center text along a sloping line</title>
    <content type="html">&lt;p&gt;I've written a countdown to my wedding, and the numbers have to be in a custom font, and positioned on top of an image in the top-right corner of the page. The numbers have to be at an angle. That's taken care of in the images 0.png to 9.png, but there are up to three digits in the number, so the images have to be placed accordingly. I've done it by creating a line called cornerLine, defined by co-ordinate arrays, and the top-right corners of the images are all positioned along this line, but if it works out better map the numbers out, you can do that.&lt;/p&gt;

&lt;pre&gt;rightOffset = 20;
topOffset = 20;
               
// refer to co-ordinates by name
// eg magicLine[point1][right]
point1 = 0;
point2 = 1;
dRight = 0;
dTop = 1; //funny name because top is a reserved word
        
cornerLine = new Array();
cornerLine[0] = new Array( 0 , 32 );
cornerLine[1] = new Array( 52 , 23 );

var now = new Date();
var wedding = new Date(2010,05,12,13,0);
var oneDay=1000*60*60*24;
days = '' + Math.round((wedding-now)/oneDay);
//days = '777';   // test figure
days = days.split('');
days.reverse();

digitPositions = new Array(days.length);

for (i=0;i&amp;lt;days.length;i++)
  digitPositions[i] = new Array();
        
if (days.length == 3)
{
  // to the furthest right position
  digitPositions[0][dRight] = Math.round(cornerLine[0][dRight]) + rightOffset;
  digitPositions[0][dTop]   = Math.round(cornerLine[0][dTop]) + topOffset;
  // to the centre position
  digitPositions[1][dRight] = Math.round((cornerLine[0][dRight] + cornerLine[1][dRight])/2 + rightOffset);
  digitPositions[1][dTop]   = Math.round((cornerLine[0][dTop] + cornerLine[1][dTop])/2 + topOffset);
  // to the furthest left postion
  digitPositions[2][dRight] = Math.round(cornerLine[1][dRight]) + rightOffset;
  digitPositions[2][dTop]   = Math.round(cornerLine[1][dTop]) + topOffset;
}
else if (days.length == 2)
{
  // a quarter of the way from the right
  digitPositions[0][dRight] = Math.round((cornerLine[0][dRight]*3 + cornerLine[1][dRight]) / 4) + rightOffset;
  digitPositions[0][dTop]   = Math.round((cornerLine[0][dTop]*3 + cornerLine[1][dTop]) / 4) + topOffset;
  // a quarter of the way from the left
  digitPositions[1][dRight] = Math.round((cornerLine[0][dRight] + cornerLine[1][dRight]*3) / 4) + rightOffset;
  digitPositions[1][dTop]   = Math.round((cornerLine[0][dTop] + cornerLine[1][dTop]*3) / 4) + topOffset;
}
else // days.length == 1
{
  // the center position
  digitPositions[0][dRight] = Math.round((cornerLine[0][dRight] + cornerLine[1][dRight])/2) + rightOffset;
  digitPositions[0][dTop]   = Math.round((cornerLine[0][1] + cornerLine[1][1])/2) + topOffset;
}
        
for (i=0; i&amp;lt;days.length; i++)
  document.write('&amp;lt;img src=&amp;quot;images/countdown/' + days[i] + '.png&amp;quot; id=&amp;quot;digit_' + i
    + '&amp;quot; style=&amp;quot;position:absolute;right:' + digitPositions[i][dRight] + 'px;top:'
    + digitPositions[i][dTop] +  'px;z-index:' + (i + 6) + '&amp;quot; /&amp;gt;');&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1191-center-text-along-a-sloping-line" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1183</id>
    <published>2010-02-19T13:55:57-08:00</published>
    <updated>2010-04-06T10:07:12-07:00</updated>
    <title>[JavaScript] Colour changer</title>
    <content type="html">&lt;p&gt;Intended to give a create a column of divs which, when clicked, changes the website to the defined colours. The strange names are defined in the style sheet (which I can't edit), but I also need to make use of the colours associated with each colour name, so I've included those in the script.&lt;/p&gt;

&lt;pre&gt;function changeColours(headerClassColour,contentClassColour,bannerDivColour)
{
	document.getElementById(&amp;quot;content&amp;quot;).className = contentClassColour + &amp;quot;-content&amp;quot;;
	document.getElementById(&amp;quot;header-layout&amp;quot;).className = headerClassColour + &amp;quot;-header&amp;quot;;
	document.getElementById(&amp;quot;page-image&amp;quot;).style.backgroundColor = bannerDivColour;
}

colours = new Array();
colours[&amp;quot;lipstickred&amp;quot;] = &amp;quot;#cc0000&amp;quot;;
colours[&amp;quot;jetsetblue&amp;quot;] = &amp;quot;#330066&amp;quot;;
colours[&amp;quot;noteasyjetorange&amp;quot;] = &amp;quot;#ff3300&amp;quot;;
colours[&amp;quot;black&amp;quot;] = &amp;quot;#000000&amp;quot;;
colours[&amp;quot;grey&amp;quot;] = &amp;quot;#575757&amp;quot;;
colours[&amp;quot;frenchnavy&amp;quot;] = &amp;quot;#336699&amp;quot;;
colours[&amp;quot;grannysmith&amp;quot;] = &amp;quot;#66CC33&amp;quot;;
colours[&amp;quot;grassgreen&amp;quot;] = &amp;quot;#339966&amp;quot;;
colours[&amp;quot;manlypink&amp;quot;] = &amp;quot;#CC0066&amp;quot;;
colours[&amp;quot;berry&amp;quot;] = &amp;quot;#990066&amp;quot;;
colours[&amp;quot;bettyblue&amp;quot;] = &amp;quot;#00CCFF&amp;quot;;
colours[&amp;quot;farmedsalmon&amp;quot;] = &amp;quot;#FF9966&amp;quot;;
colours[&amp;quot;graygreen&amp;quot;] = &amp;quot;#cccc00&amp;quot;;
colours[&amp;quot;trueblue&amp;quot;] = &amp;quot;#0099FF&amp;quot;;
colours[&amp;quot;raspberry&amp;quot;] = &amp;quot;#CC3333&amp;quot;;
colours[&amp;quot;poppy&amp;quot;] = &amp;quot;#FF6633&amp;quot;;
colours[&amp;quot;polomint&amp;quot;] = &amp;quot;#33CC99&amp;quot;;
colours[&amp;quot;peagreen&amp;quot;] = &amp;quot;#339966&amp;quot;;
colours[&amp;quot;bakedbean&amp;quot;] = &amp;quot;#009999&amp;quot;;
colours[&amp;quot;frogsleg&amp;quot;] = &amp;quot;#00CC33&amp;quot;;
colours[&amp;quot;irnbruchew&amp;quot;] = &amp;quot;#FF9900&amp;quot;;
colours[&amp;quot;tangerine&amp;quot;] = &amp;quot;#FF6600&amp;quot;;
colours[&amp;quot;verdigreen&amp;quot;] = &amp;quot;#669966&amp;quot;;
colours[&amp;quot;watermelon&amp;quot;] = &amp;quot;#FF6666&amp;quot;;
colours[&amp;quot;sbs&amp;quot;] = &amp;quot;#00AECB&amp;quot;;
colours[&amp;quot;yellow&amp;quot;] = &amp;quot;#FCE016&amp;quot;;

for(var colourName in colours)
{
	var colourValue = colours[colourName];
	
	document.write('&amp;lt;div style=&amp;quot;height:20px;background-color:'
				+ colourValue + '&amp;quot; onclick=&amp;quot;changeColours(\'' + colourName
				+ '\',\'' + colourName + '\',\'' + colourValue + '\')&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;');
}

// set the colour on page loading
changeColours(&amp;quot;jetsetblue&amp;quot;,&amp;quot;jetsetblue&amp;quot;,colours[&amp;quot;jetsetblue&amp;quot;]);
&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1183-colour-changer" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1173</id>
    <published>2010-02-09T14:02:32-08:00</published>
    <updated>2010-07-14T04:49:48-07:00</updated>
    <title>[PHP] Get Title of HTML page</title>
    <content type="html">&lt;p&gt;Extract and return the bits between the &amp;lt;title&amp;gt; and &amp;lt;/title&amp;gt; tags of an html page.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function get_title($html_page)
{
  // split the page into 3 sections, with the &amp;lt;title&amp;gt; and &amp;lt;/title&amp;gt; tags as delimiters.
  $split_page = preg_split(&amp;quot;%&amp;lt;/?title[^&amp;gt;]*&amp;gt;%&amp;quot;,$html_page);

  if (sizeof($split_page) == 3)
    return $split_page[1];
  else
    return &amp;quot;No title&amp;quot;;
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1173-get-title-of-html-page" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor438764</id>
    <published>2010-02-09T10:17:03-08:00</published>
    <title>[PHP] On Return the appropriate file handle</title>
    <content type="html">&lt;p&gt;Thanks for that. It does make the code a lot simpler setting a default $type, but the ideal is that if the user leaves it blank, the function detects how the file has been opened (obviously it can be opened in more that one way, but this would be rare, and they shouldn't be leaving it blank if this is the case). Having said all that, I've had a go at refactoring your solution further!!!&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function get_handle($type='!') // $type set to something invalid, so that user explicitly has to declare it.
{
  // If everything is ok, just return what was requested.
  $mode = 'fh'.$type;

  if ( isset($this-&amp;gt;$mode) )
    return $this-&amp;gt;$mode;

  $types = array('r' =&amp;gt; 'reading', 'w' =&amp;gt; 'writing', 'a' =&amp;gt; 'appending');

  // Work out what kind of error to return
  if (in_array($type,array_keys($types)))
    user_error(&amp;quot;File not open for &amp;quot;.$types[$type]);
  else
    user_error(&amp;quot;Invalid type&amp;quot;);
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1171-return-the-appropriate-file-handle/refactors/438764" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1171</id>
    <published>2010-02-09T00:35:08-08:00</published>
    <updated>2010-02-09T10:17:05-08:00</updated>
    <title>[PHP] Return the appropriate file handle</title>
    <content type="html">&lt;p&gt;This method feels like it's way more complicated than it should be.&lt;/p&gt;

&lt;p&gt;Update: added &amp;lt;?php to colourise.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function get_handle($type)
{
	if (type==&amp;quot;r&amp;quot;)
	{
		if (isset($this-&amp;gt;fhr)) return $this-&amp;gt;fhr;
		else user_error(&amp;quot;File not open for reading&amp;quot;);
	}
	elseif (type==&amp;quot;w&amp;quot;)
	{
		if (isset($this-&amp;gt;fhw)) return $this-&amp;gt;fhw;
		else user_error(&amp;quot;File not open for writing&amp;quot;);
	}
	elseif (type==&amp;quot;a&amp;quot;)
	{
		if (isset($this-&amp;gt;fha)) return $this-&amp;gt;fha;
		else user_error(&amp;quot;File not open for appending&amp;quot;);
	}
	elseif (!isset($type))
	{
		if (isset($this-&amp;gt;fhr)) return $this-&amp;gt;fhr;
		elseif (isset($this-&amp;gt;fhw)) return $this-&amp;gt;fhw;
		elseif (isset($this-&amp;gt;fha)) return $this-&amp;gt;fha;
		else user_error (&amp;quot;File not open&amp;quot;);
	}
	else user_error(&amp;quot;Invalid type&amp;quot;);
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1171-return-the-appropriate-file-handle" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor436009</id>
    <published>2010-02-03T11:16:34-08:00</published>
    <title>[VB.NET] On Many-to-many list box interface</title>
    <content type="html">&lt;p&gt;Oh, should have said its in MS Access 2007. Thanks for the hint... I'll have a look at adding a sub when I get time.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1157-many-to-many-list-box-interface/refactors/436009" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1157</id>
    <published>2010-01-25T13:56:46-08:00</published>
    <updated>2010-02-03T11:16:35-08:00</updated>
    <title>[VB.NET] Many-to-many list box interface</title>
    <content type="html">&lt;p&gt;VBA, not VB.NET.&lt;/p&gt;

&lt;p&gt;This is to provide a simple many-to-many join table UI linking Ambassadors and Events. The list boxes are bound to queries, one of which gives the names of the ambassadors linked to the current event, and the other one gives all the ambassadors by looking for a null link with the first query. I tried to do this with as little extra code as possible!&lt;/p&gt;

&lt;p&gt;Gone overkill with comments because code may be accessed by people who know very little VBA, if any.&lt;/p&gt;

&lt;pre&gt;Private Sub cmdAddAmbassador_Click()
' Run when the user clicks the &amp;quot;&amp;lt;&amp;quot; button to add an ambassador to an event

  ' Need to make sure varItem is the right kind of object
  Dim varItem As Variant
  
  ' Get rid of those pesky &amp;quot;You are about to append 1 row&amp;quot; warnings.
  ' This also supresses important error messages, so should be commented
  ' out for debugging.
  DoCmd.SetWarnings (False)

  ' Loop through all the items selected in the &amp;quot;Available ambassadors&amp;quot; list
  For Each varItem In Me.lstAvailableAmbassadors.ItemsSelected
    
    ' Add the selected person to the current event, by creating a new record
    ' in the join table
    DoCmd.RunSQL &amp;quot;INSERT INTO AmbassadorWork VALUES (&amp;quot; &amp;amp; Me.ID &amp;amp; &amp;quot;,&amp;quot; _
    &amp;amp; Me.lstAvailableAmbassadors.Column(3, varItem) &amp;amp; &amp;quot;,NULL,NULL);&amp;quot;
  
  Next

  ' Reload the list boxes so that they contain up-to-date information
  Me.lstAvailableAmbassadors.Requery
  Me.lstWorkingAmbassadors.Requery
End Sub

Private Sub cmdRemoveAmbassador_Click()
' Run when the user clicks the &amp;quot;&amp;gt;&amp;quot; button to remove an ambassador from an event
  
  Dim varItem As Variant
  DoCmd.SetWarnings (False)

  ' Loop through selected items in the list
  For Each varItem In Me.lstWorkingAmbassadors.ItemsSelected
  
    ' Remove the selected person from the current event by deleting the record
    ' which links the person with this event from the join table
    DoCmd.RunSQL &amp;quot;DELETE FROM AmbassadorWork WHERE (&amp;quot; &amp;amp; &amp;quot;EventID = &amp;quot; &amp;amp; Me.ID _
    &amp;amp; &amp;quot; AND AmbassadorID = &amp;quot; &amp;amp; Me.lstWorkingAmbassadors.Column(3, varItem) &amp;amp; &amp;quot;);&amp;quot;
    
  Next

  ' Reload the list boxes so that they contain up-to-date information
  Me.lstAvailableAmbassadors.Requery
  Me.lstWorkingAmbassadors.Requery
End Sub&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1157-many-to-many-list-box-interface" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code531</id>
    <published>2008-10-10T22:29:19-07:00</published>
    <updated>2009-04-14T21:44:53-07:00</updated>
    <title>[Ruby] brute-force password cracker</title>
    <content type="html">&lt;p&gt;Just a quick-and-dirty for a brute force attack. I made this to break into my router, because I wanted to check the settings (so a hard system restore wouldn't work because it would wipe them). I've included a little test method instead of the actual login script and reduced the selection of characters (characters_to_use) to help with speed of testing. I'd be interested to see what people come up with.&lt;/p&gt;

&lt;pre&gt;#!/bin/ruby

characters_to_use=&amp;quot;ABCD&amp;quot;
minimum_password_length=5
maximum_password_length=15

def password_correct?(test_password)
# This is just for testing. There's no need to alter this bit!
  return true if test_password == &amp;quot;DCBAAAA&amp;quot;
  return false
end

char_array=characters_to_use.split(//)
is_access_granted = false

(minimum_password_length..maximum_password_length).each do |password_length|  # loop to gradually increase password length
  combinations=char_array.length**password_length

  (0..combinations-1).each do |perm_number| # loop through combinations for a given length
    password=&amp;quot;&amp;quot;
    (1..password_length).each do |char_number| # loop through characters
      char_reference = (perm_number / char_array.length**(char_number-1)).floor % char_array.length
      character = char_array[char_reference]
      password &amp;lt;&amp;lt; character
    end

    if password_correct?(password)
     puts &amp;quot;#{password} | Access Denied | #{perm_number} / #{combinations}&amp;quot;
    else
      puts &amp;quot;#{password} | Access Granted&amp;quot;
      is_access_granted = true
      break
    end
  end
  break if is_access_granted
end&lt;/pre&gt;</content>
    <author>
      <name>Nathan</name>
      <email>njmacinnes@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/531-brute-force-password-cracker" rel="alternate"/>
  </entry>
</feed>

