Skip to content

Instantly share code, notes, and snippets.

@adomokos
adomokos / group_by_spec.rb
Created March 5, 2014 04:12
Recognizing Ruby's group_by in the imperative code
class Location
attr_reader :city, :user
def initialize(city, user)
@city = city
@user = user
end
end
class User
attr_reader :name
@adomokos
adomokos / string_calculator_spec.rb
Last active August 29, 2015 14:12
String Calculator Kata in Ruby - using enumerable methods
# Solving the String Calculator Kata
# http://osherove.com/tdd-kata-1/
class StringCalculator
def self.add(numbers)
return 0 if numbers.empty?
numbers.split(",").map(&:to_i).reduce(:+)
#splitted_array = numbers.split(",")
@adomokos
adomokos / core_test.clj
Created May 15, 2015 18:53
Solving the Roman Numeral Kata in Clojure
(ns roman-numerals.core-test
(:require [clojure.test :refer :all]))
(def mapping {
0 ""
1 "I"
4 "IV"
5 "V"
9 "IX"
10 "X"
using System;
using Machine.Specifications;
using Given = Machine.Specifications.Establish;
using When = Machine.Specifications.Because;
using Then = Machine.Specifications.It;
namespace TryMSpec {
public class TicketCalculator{
private decimal _ticket_price = 11m;
public void StartPurchase(int runtime, DayOfWeek day, bool isParquet, bool is3D) {}
// TotalCalculator.js
function TotalCalculator() {
}
TotalCalculator.prototype.calculate = function(quantity, price) {
var _quantity = parseInt(quantity);
if (isNaN(_quantity))
return 'N/A';
var _price = parseInt(price.replace('$', ''));
@adomokos
adomokos / JavaScript Calculator with Closure
Created November 7, 2010 04:01
A JavaScript Calculator with Closure
/*
function Calculator() {
this.add = function(input) {
var result = 0;
for(i = 0; i<input.length; ++i) {
result += input[i];
}
return result;
}
@adomokos
adomokos / Error Publisher in Haml
Created November 10, 2010 03:08
Since this was - rightly - removed from Rails 3
module ApplicationHelper
def error_publisher(model)
# Thanks Joe!
return unless model.errors.any?
item_errors = ''
model.errors.full_messages.each do |e|
item_errors += "<li>#{e}</li>"
end
@adomokos
adomokos / tracks_controller.rb
Created April 15, 2011 03:50
This code is to demonstrate how I use test doubles for Rails.
class TracksController < ApplicationController
def index
signed_in_user
end
def new
@track = Track.new
end
def create
@adomokos
adomokos / string_calculator_spec.coffee
Created October 2, 2011 12:37
Hackibou (09/01/2011) CoffeeScript file - working on the String Calculator kata
class StringCalculator
calculate: (input) ->
@validateArgument(input)
result = 0
result += parseInt(splitValue) for splitValue in @splitValues(input)
result
validateArgument: (input) ->
@adomokos
adomokos / sort_with_other.rb
Created October 5, 2011 19:40
A special kind of sorting algorithm, where "Other" has to be the last item when sorted
require 'ostruct'
module SortWithOther
def <=>(other)
if self.send(sort_field).downcase == "Other".downcase
1
elsif other.send(sort_field).downcase == "Other".downcase
-1
else
self.send(sort_field) <=> other.send(sort_field)