TitusD

Joe Yates' Blog

My Everyday Find Command

When I’m searching for files, I use this function:

It searches for files and directories with partial matches of the first parameter:

1
2
$ f 26
./db/migrate/20121003094826_add_foo_to_bar.rb

If I supply a second parameter, it is taken as the directory to search in:

1
2
3
$ f 26 ..
../api/db/migrate/20121003094826_add_foo_to_bar.rb
../redmine/db/migrate/20091017212644_add_missing_indexes_to_messages.rb

Ruby Bareword Assignment and Method Calls With Implicit Self

Problem

If I do this:

Problem - problem.rb
1
2
puts foo
foo = 3

there is always the doubt whether I’m accessing a local variable, or calling methods foo and foo=.

TL;DR

When you want to call an instance’s own methods, use self:

TL;DR - tldr.rb
1
2
self.foo             # Calls foo
self.foo = 'bar'     # Calls foo=

HTML History API

The HTML5 history API allows AJAX-based sites to avoid “breaking the back button”. Every time you update the page, you store the new content is the window.history object. When the user presses the back button, you retrieve the current item from window.history and update the page with it.

Overriding the Insertion Operator for C++ Template Classes

Dump an Active Record object to Standard Out - dump_active_record_object.cpp
1
2
Person person( 123 );
std::cout << person << endl;

While working on my C++ ActiveRecord implementation, I had a few problems implementing the insertion operator for the main ActiveRecord::Base class.

The class ActiveRecord::Base class is a template class, and the problem was how to correctly declare the operator.

At the time I was unable to find examples on the Internet, so I thought I’d provide my own.

Rake::Builder

Use Rake::Builder as a Make Replacement - build.sh
1
$ rake build

I’ve put together a rake-based system for building C and C++ projects.

I’ve called it Rake::Builder, it’s available on GitHub here.

Sinatra: A Real Site

The Site

I have a young relative who draws a lot of comic strips, and thinking it would be nice if he could publish them, I looked around for an Open Source system.
I wanted something that was as near to XKCD as possible - latest comic, next/previous and random.
Not finding anything that was both pretty and ultra-simple, I decided to write my own.
The version for my relative is here - the system will be up on GitHub as soon as I finish making it configurable.

Object Representation in C++

Member variables

Each C++ object needs to hold its instance data. But, there is often not a perfect match between the sum of the byte sizes of the member variables and the size of an object: The space occupied will often be greater than the sum of the sizes of the member variables.

The reasons are:

  • compiler-specific alignment issues: in order to speed up access, a few bytes can be wasted for each instance to ensure that all the members are aligned on a word boundary,
  • the presence of a pointer to the virtual function table,
  • empty classes.