Geeks == Coolness

Custom matcher for Ruby collections

by on May.20, 2012, under Ruby

Need to check if every element of a collection has some property? No problem.

1. Add this custom matcher to your Ruby tests

module CollectionMatchers
class AllMatchProperty
def initialize(expected_property)
@expected_property = expected_property
def matches?(target_collection)
@target_collection = target_collection
if !@target_collection.respond_to?(:each_with_index)
raise ArgumentError “Collection needs to have each_with_index method”
@target_collection.each_with_index do |elem, i|
if !
@unexpected_element, @index = elem, i
return false
def failure_message_for_should
“Element #{@unexpected_element.inspect} at index #{@index} does not have tested property”
def failure_message_for_should_not
“Element #{@unexpected_element.inspect} at index #{@index} has tested property”
def all_be(expected)
def all_have(expected)

2. Use it like this:

it “should filter per language” do
in_english = lambda { |a| Article.find(a.article_id).language_id == @ }
@my_articles.should all_be(in_english)

Comments Off on Custom matcher for Ruby collections more...

Having Fun with SQL

by on Jul.10, 2011, under SQL

A month ago I changed jobs and now I’m learning everything from the beginning (or that’s how it feels): Ruby, Rails, Git, Unix and MySQL. My first task turned out to require some fancy SQL skills so I thought to share what I learned. I couldn’t find anything quite like what I needed so maybe this will help somebody else.

Here is the problem: gather some statistics about items and their properties with just one SQL query. The database looks something like this:

  • There is a Groups table. Each group has any number of Subgroups (different table with a foreign key).
  • Each subgroup can have multiple items. The tricky part is that an item can belong to multiple subgroups at the same time (many to many relationship). This is expressed by having Map table that holds a subgroup key and an item key.
  • Items table contains various information about items (e.g if they are public or private, up to date or not, etc.)
  • Finally each item has certain fixed number of properties (stored in Properties table).

Information needed is this: for every group ID and every possible property, count number of items belonging to that group with that property and desired characteristic (e.g public).The result should looks something like: GroupID1, Property1, # of items, # of public items, # of public, up-to-date items.

The problem with creating simple join with all the interesting tables is that there are possible duplicates. E.g. if an item belongs to all subgroups of a given group it will be counted multiple times (once for each subgroup). This would make totals wrong. Here is what I ended up with:

SELECT,, COUNT( AS total_items,
COUNT(IF(items.public = 1 AND items.uptodate = 1, 1, NULL)) AS total_uptodate,
COUNT(IF(items.public = 1, 1, NULL)) AS total_public FROM Groups
SELECT DISTINCT AS group_id, AS article_id FROM Groups
INNER JOIN Subgroups ON Subgroups.group_id =
INNER JOIN Map ON Map.subgroup_id =
INNER JOIN Items ON = Map.item_id
) group_item ON group_item.group_id =
INNER JOIN Items ON = group_item.article_id
INNER JOIN Properties ON Properties.article_id =

The trick was to create sort of a temporary table that contained info about each group and article that group contained without any duplicates (group_item), which was achieved with ‘SELECT DISTINCT’ statement and then use that table to join with others to create correct totals. If there are easier ways to get same info in an easier way please do let me know!

Comments Off on Having Fun with SQL more...

Perfect Valentine’s Day Gift for Your Geek

by on Feb.08, 2011, under geeks

Is there a special geek in your life that you’d like to surprise with something truly *unique* for Valentine’s Day? Maybe you thought about something over-the-top like naming a newly discovered star by her or his name? But is that special enough? Are you striving for something that only True Geeks™ will recognize and appreciate?

Fear not, there is a perfect solution for your dilemma: GUID dedication!

I mean, it’s really hard to find something more unique than a GUID, right? So fire up your guidgen.exe, find that one special GUID that looks just right, and wrap it into a nice package. Maybe something like this:


Here is a link to my template that you can download and customize. And don’t forget to generate your own GUID, we don’t want any duplicates going around, right?

Disclaimer: Author accepts no liability for the success of above described gift idea, or for the consequences of any actions taken on the basis of the information provided. You might want to make sure you are dealing with a True Geek™ first and have a backup gift just in case. That reminds me…

Comments Off on Perfect Valentine’s Day Gift for Your Geek : more...

My favorite interop resources

by on Jan.25, 2011, under interop

After working on interop between managed and native code (a.k.a. COM interop and pinvoke) for five years I collected some useful pointers that I always share when somebody wants to learn more about this area. Here is what I have in no particular order.

1. Adam Nathan’s interop bible: the ultimate interop resource. It’s so huge the publisher split it into two volumes. If you are planning to write a lot of interop code this is must have.

.NET and COM: The Complete Interoperability Guide (2 Volume set)

2. MSDN article that provides some high level guidance on how to use interop in different directions with performance and security implications. Note that this was written years ago so some performance numbers and architecture details might be different now.

3. Various MSDN topics that share the same top level node: Interoperating with Unmanaged Code

Overview of Interop Marshaling with nice pictures.

Basic info about Runtime Callable Wrappers (RCWs) and COM Callable Wrappers (CCWs).

Marshaling Rules a.k.a. Why did my stack get corrupted?

Memory Management and how interop marshaler tries to determine which memory to free.

When to help marshaler by sprinkling InAttribute and OutAttribute over pinvoke signatures.

Performance implications of marshaling: Copying vs. Pinning. Lots more pretty pictures.

Handy table with list of native types and their corresponding managed equivalents.

Pinvoke samples demonstrating various marshaling techniques. (Follow In This Section links.)

Use all this marshaling knowledge to write your own COM type description. A.k.a. who needs TlbImp.exe?

How to use SafeHandles to prevent native resource leaks.

Need to customize marshaling behavior? A.k.a. how do I marshal native IStream to managed Stream.

4. Advanced in-depth topics covered in Chris Brumme’s old blog. E.g. everything you ever wanted to know about COM apartments and threading and why there are only handful of people who can actually make sense of it.

5. Need to call a particularly nasty Win32 API from managed code? wiki to the rescue! A wonderful community resource to find and contribute pinvoke definitions, COM interfaces, and various structures used as arguments. Set up by Adam Nathan himself!

Happy coding and don’t forget to turn on those Managed Debugging Assistants (MDAs). They are specifically designed to help you debug the most common interop errors.

Comments Off on My favorite interop resources :, , , more...

Hello (Blogging) World!

by on Oct.03, 2010, under Uncategorized

I’m a bit late to the party but better late than never, eh? First some introductions.

My name is Sonja Keserovic and I’m a development lead on the Web Platform Team, which is responsible for technologies like ASP.NET, MVC, IIS, WebPages, etc. I’ve been a Microsoftee for almost ten years (my anniversary is coming up in a couple of weeks!) starting in Common Language Runtime (CLR) team, with a couple years stints in Windows and Windows Live, and C# compiler. Since I started in the CLR team even before v1 version of .NET Framework was out, I have a soft spot for .NET languages and technologies so obviously my blog is on a .net domain. (Should I get .com one too or just leave room for lots of hilarity down the road?)

Having said this, my dear geek reader, you are probably wondering what I’m doing hosting this blog on WordPress and dabbling in PHP? While working on ASP.NET WebPages I realized I really don’t know much about other non-Microsoft web technologies out there and that I should find ways to fix this. At the same time I also wanted to start a blog for a while so hosting it on WordPress seemed like a logical choice. I get exposure to a popular PHP blog engine and an incentive to dig deeper around it as I tweak whatever strikes my fancy on this blog.

So far it’s been very easy to set everything up with WordPress (though I’m still playing with all the options, themes, etc.) and Windows Live Writer totally impressed me by pointing out exact option and exact settings page(!) to enable remote publishing. And yes, I know this is Microsoft application and I’m on a quest to learn other technologies, but I heard it’s best so I had to give it a try. And it’s written in managed code. :)

Until next time…

Comments Off on Hello (Blogging) World! more...