I'd like to start posting code on my blog so I'm testing out a syntax highlighter plugin. This post will likely go through several iterations as I see what it can do and how to do it.
Here's some ruby:
RUBY:
-
def addPages(urls)
-
return false unless urls
-
urls.each{ |u| @queue <<PageFactory.newPage(u) }
-
@queue.uniq! # Get rid of dupes.
-
end
Here's some Perl:
PERL:
-
my @text = ( 'here', 'are', 'some', 'words' );
-
my $offset = 0;
-
-
# Convert a string to "ChOpPeD cAsE"
-
sub choppedCase
-
{
-
-
-
for (my $i = $offset; $i <$char_count; $i+=2)
-
{
-
}
-
-
# Track odd length words so we constantly alternate in a string
-
if ($char_count % 2)
-
{
-
$offset = $offset == 0 ? 1 : 0;
-
}
-
}
Here's some PHP
PHP:
-
// Pattern to match the directory path that a file is in
-
$dir_pattern = "@(/.*)/[^/]*$@";
-
-
// Take a path that's (possibly) relative to the directory that the script is executing in
-
// and convert it to an absolute path.
-
function absolutize($path)
-
{
-
global $dir_pattern;
-
-
// Only operate on paths that are relative
-
-
foreach ($path_bits as $bit)
-
{
-
if ($bit == '' || $bit == '.')
-
continue;
-
if ($bit == '..') {
-
continue;
-
}
-
$elms[]= $bit;
-
}
-
// Push an empty string on top so that we get a leading / when we implode
-
}
I just realized that I've coded in all three of these languages this week - plus JavaScript (can't find a good sample to post).
Update
JavaScript
JAVASCRIPT:
-
// Add commas to a long number to make it more readable.
-
// Currently only supports whole numbers.
-
var addCommas = function(aNumber)
-
{
-
// Convert our number to an array and reverse it so that
-
// we can easily work from most significant digits down
-
// to least significant
-
var bitsR = (aNumber.toString().split('')).reverse();
-
for (var i = bitsR.length - 1; i>2; i--)
-
if (i % 3 == 0)
-
bitsR.splice(i, 0, ',');
-
// Return our array with commas added back to the proper order
-
// for display on the screen.
-
return bitsR.reverse().join('');
-
};
Entries (RSS)