Skip to content

Instantly share code, notes, and snippets.

@farnoy
farnoy / rbtree.rb
Created March 19, 2012 18:37
Implementation of a red black tree in ruby
#!/usr/bin/env ruby
#define BLACK 0
#define RED 1
typedef struct tree_el{
int value;
struct tree_el *left;
struct tree_el *right;
int color;
} node;
def mergesort(list)
return list if list.size <= 1
mid = list.size / 2
merge mergesort(list[0, mid]), mergesort(list[mid, list.size])
end
def merge(left, right)
sorted = []
until left.empty? or right.empty?
if left.first <= right.first
@farnoy
farnoy / fields.go
Created March 16, 2012 10:58
Reflection assignment for struct fields in Go
package fields
import (
"fmt"
"reflect"
)
// Provides a shortcut and prettier syntax for map definitions.
type Assigns map[string]reflect.Value
#!/usr/bin/env ruby
require 'gosu'
module GoUp
def self.update(obj, window)
obj.move(0, -1)
end
end

Setup

A -- (master)
    \--> B (remove-blanks)

The problem

Master repo awaits user contributions. Many ways to achieve that, let's review some of them.

Patches

flexmock(User).should_receive(:find).with('42').and_return(jane) # Flexmock
User.should_receive(:find).with('42').and_return(jane) # Rspec
User.expects(:find).with('42').returns {jane} # Mocha
User.should_receive(:find).with('42') {jane} # Rspec using return value blocks
mock(User).find('42') {jane} # RR
@farnoy
farnoy / main-window.cc
Created March 10, 2012 13:05
Print first n lines from a file
void MainWindow::show_message()
{
std::stringstream title, content;
title << "The first [" << m_spin_button.get_value_as_int() << "] lines";
Gtk::MessageDialog dialog(title.str());
std::ifstream file;
file.open(m_file_chooser_button.get_filename().c_str(), std::ifstream::in);
char buffer[512];
short i = 0;
task :minitest do
path = File.expand_path('test', Rails.root)
require File.expand_path('test_helper', path)
Dir[path + '/*_test.rb'].each do |file|
require file
end
end
kuba:Blog$ tree spec/
spec/
├── controllers
│   └── posts_controller_spec.rb
├── helpers
│   └── posts_helper_spec.rb
├── models
│   └── post_spec.rb
├── requests
│   └── posts_spec.rb