Ruby: Comparing an Object’s Class in a Case Statement

December 7, 2011 Leave a comment

Thanks to the o.b blog for this ruby tidbit. If you want to use an object’s class in a case statement in ruby the intuitive

case obj.class
when String: "blah"
when Fixnum: "foo"
end

doesn’t actually work? Why not you ask? Surely String == String will return true. Alas it seems we are now victims of ruby being too clever for it’s own good; if you’ve put class objects in the when clause you are obviously (?) wanting to compare the class of the object in the case statement. So ruby is helpfully inserting an extra .class call for you.

So if you want it to actually work

case obj
when String: "blah"
when Fixnum: "foo"
end

Categories: Uncategorized Tags:

Setting up PostgreSQL + postgis for rails on ubuntu

November 24, 2011 Leave a comment

I know there are plenty of guides out there for this, but this is to allow me to tie together all the relevant info I’ve found about this.

I already had postgres installed and configured so I just had to install the postgis extensions


sudo apt-get install postgresql-8.4-postgis

The following is adapted from this guide to use the correct locations for postgres 8.4


sudo su - postgres
createdb -E UTF8 template_postgis # Create the template spatial database.
createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
psql -d osm -f /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql
psql -d osm -f /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
cat <<EOS | psql -d template_postgis
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';
REVOKE ALL ON SCHEMA public FROM public;
GRANT USAGE ON SCHEMA public TO public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.geometry_columns TO PUBLIC;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.spatial_ref_sys TO PUBLIC;
GRANT ALL ON geometry_columns TO PUBLIC;
GRANT ALL ON geography_columns TO PUBLIC;
GRANT ALL ON spatial_ref_sys TO PUBLIC;
VACUUM FULL FREEZE;
EOS

This guide shows how to setup your database.yml but you are probably better off following the advice from the spatial_adapter wiki. Your database.yml should look like this


test:
adapter: postgresql
encoding: utf8
database: myproject_test
template: template_postgis

Then you can create your db and perform any migrations with:


rake db:create
rake db:migrate

fatal: open database /etc/aliases.db

November 7, 2011 Leave a comment

Thanks to these guys (http://www.scriptinstallation.in/fatal_database_aliases.html) I was able to solve this with

On a new postfix mail server installation, i got following error on console

Jul 11 19:19:32 freebsd postfix/local[654]: fatal: open database /etc/aliases.db: No such file or directory

The error is because postfix can’t find “/etc/aliases.db”. Problem is fixed with by recreating aliases.db file.

postfix stop
rm -f /etc/aliases.db
newaliases
postfix start

Categories: Uncategorized Tags: , ,

My first github repo

October 22, 2011 Leave a comment

More out of curiosity than anything else I decided to see if I could create a ruby API for the Brisbane Translink GO card, I’ve pushed it up onto github (https://github.com/jamiecook/gocard-ruby-api) if anyone is interested.

Categories: git, github, ruby Tags:

Using ruby to fix line endings

December 6, 2010 Leave a comment

I just f*@&ed up my svn entries file by changing the line endings during a manual edit (note: don’t try that at home). But anyways the fix was to load the file in binary mode, strip the spurious line endings and re-write the file in binary mode

e = []; File.open('entries', 'rb') {|f| e = f.read }
File.open('entries', 'wb') {|f| f.write(e.gsub(/\x0D\x0A/, "\x0A") }

Categories: ruby Tags: , ,

/3GB switch in Windows [Vista,7]

December 1, 2010 Leave a comment

Thanks to Volker for this little explanation of the changed process for enabling large memory space for user mode applications. Instead of adding /3GB to your boot.ini as you did in windows XP, you now execute the following command from an administrator command prompt:

BCDEDIT.EXE /Set IncreaseUserVa 3072

Categories: Uncategorized Tags: ,

Boost shared_ptr method make_shared

April 28, 2010 2 comments

I was reading a post about a new feature in Visual Studio 2010 that would allow for syntax like this for shared_ptr

auto sp = make_shared<T>(args)

and I thought there should already be something like that in boost; turns out there was and I just wasn’t using it. So I’m posting an example of how this can make your code a bit cleaner and neater; below is a before and after

tuple<int,int,int,int> key = make_tuple(stopNrA, stopNrB, transitLineNr, time);
if (!hasElement(m_routeSegments, key))
  m_routeSegments[key] = shared_ptr(new RouteSegment(param1, param2));
return m_routeSegments[key];

and then after using make_shared and auto

auto key = make_tuple(stopNrA, stopNrB, transitLineNr, time);
if (!hasElement(m_routeSegments, key))
  m_routeSegments[key] = make_shared<RouteSegment>(param1, param2);
return m_routeSegments[key];

The differences aren’t huge but it’s easy to see how things like the typing of a temporary key value and the boiler plate of constructing an object and then passing it to the constructor of a shared_ptr can unnecessarily clutter code making it less readable.

Categories: shared_ptr Tags: , ,

Boost Unit Test Reporting

February 17, 2010 Leave a comment

In case it’s not obvious, I’m doing some work with boost today upgrading from 1.34.1 -> 1.42.0. I just came across this following error while running my tests

<pre>C:\projects\svn\bdk\ot51>ZenithBase_test.exe –report_level=yes
Test setup error: invalid report level yes</pre>

Thats funny because ‘yes’ used to be a valid option and I can’t see what they’ve changed it to or even what the possible options are anywhere on the internet… so I decided to put them down here based on my digging through the source:

* confirm – CONFIRMATION_REPORT,
* short – SHORT_REPORT,
* detailed – DETAILED_REPORT,
* no – NO_REPORT

Categories: 1.42.0, boost, c++ Tags: , ,

BOOST_STATIC_ASSERT incomplete type not allowed

February 17, 2010 Leave a comment

I’ve recently been trying to compile the newest boost library (1.42.0) using the Intel 11.0 compiler and came across this error in code which previously compiled using 1.34.1

1>../../deps/boost_1_42_0/boost/test/floating_point_comparison.hpp(229): error: incomplete type is not allowed
1>          BOOST_STATIC_ASSERT( !is_integral::value );
1>          ^
1>          detected during:
1>            instantiation of "boost::test_tools::predicate_result boost::test_tools::check_is_close_t::operator()(FPT1, FPT2, boost::test_tools::percent_tolerance_t, boost::test_tools::floating_point_comparison_type) const [with FPT1=int, FPT2=int, ToleranceBaseType=double]" at line 523 of "../../deps/boost_1_42_0/boost/test/test_tools.hpp"
1>            instantiation of "bool boost::test_tools::tt_detail::check_frwd(Pred, const boost::unit_test::lazy_ostream &, boost::test_tools::const_string, size_t={unsigned int}, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, const Arg0 &, const char *, const Arg1 &, const char *, const Arg2 &, const char *) [with Pred=boost::test_tools::check_is_close_t, Arg0=int, Arg1=int, Arg2=boost::test_tools::percent_tolerance_t]" at line 26 of
1>                      "..\tests\Timing\VlcTimingProfile_test.cpp"

incomplete type huh, what the hell could that be about? Well it turns out that this is just a very *un*informative error which is generated when the assert evaluated to false. In my case it was caused by calling BOOST_CHECK_CLOSE(int,int,float), when I changed this to BOOST_CHECK_CLOSE(float,float,float) it compiled fine… but the error message didn’t really reflect where the error was at all.

San Francisco

February 12, 2010 Leave a comment

In San Fran we stayed just off Union Square (centre of town) in a hotel called the Mosser which was the smallest room (outside of south Korea) I’ve ever been in. The toilet so small that the basin couldn’t fit in and had to be accommodated in the main room :) This was more than made up for by it’s great location however; we would literally walk out of the hotel into the hustle and bustle of downtown San Francisco. While in town we did the things typically expected of tourist, we drove across the Golden Gate bridge and spent some time exploring the Golden Gate park which is an enormous reserve to the west of the city which stretches right down to the Pacific ocean. We also had some fun taking a trip out to Alcatraz island which quite apart from it’s history as a jail has a very rich military history dating back to the Mexican occupation of California. We then jumped on a guided tour of the town given by a talkative chap named Dom who drove us around in an open top 1924 Ford motorcar which had been refitted with a new gas powered engine (they are big on green over in SF) which could drag it up and down San Fran’s incredible hills.

Follow

Get every new post delivered to your Inbox.