Skip to content

Instantly share code, notes, and snippets.

require 'rspec/core/formatters/progress_formatter'
require 'awesome_print'
class MyFormatter < RSpec::Core::Formatters::ProgressFormatter
def start_dump
super
current_hash = examples_to_hash(examples)
if File.exist? 'result.txt'
lines = read_log 'result.txt'
previous_hash = lines_to_hash lines
class Hash
# {'x'=>{'y'=>{'z'=>1,'a'=>2}}}.leaves == [1,2]
def leaves
each_value.flat_map do |value|
value.respond_to?(:leaves) ? value.leaves : [value]
end
end
end
using NUnit.Framework;
namespace Calculator.Test
{
[TestFixture]
public class CalculatorTest
{
[TestCase( 2, 2, Result = 4)]
[TestCase( -3, -4, Result = -7)]
[TestCase( 0, 0, Result = 0)]
@DNNX
DNNX / erbug.rb
Created September 19, 2011 15:38
ERB+Windows bug (extra newlines)
require 'erb'
arr = [1,2,3]
context = instance_eval('binding')
src = ::File.binread('template.rb.tt')
content = ERB.new(src, nil, '-', '@output_buffer').result(context)
print content
@DNNX
DNNX / TestWithMRUnit.java
Created November 16, 2011 11:06
How to test Hadoop Mappers with and without MRUnit
@Test
public void shouldMapDocument() throws IOException, InterruptedException {
new MapDriver(new DocumentMapper())
.withInput(createDocumentForTest())
.withOutput(expected0)
.withOutput(expected1)
.runTest();
}
@DNNX
DNNX / glob.bat
Created December 15, 2011 14:18
Glob expanding: Windows vs Unix vs PowerShell
SETLOCAL ENABLEDELAYEDEXPANSION
SET FILES=
FOR %%A IN (%1\*.gz) DO ( SET FILES=!FILES! %%A )
echo !FILES!
@DNNX
DNNX / gist:4283465
Created December 14, 2012 07:41
Different behaviour of default initializer in Ruby 1.9.2 and Ruby 1.9.3
➜ ~ rvm use 1.9.2 && ruby -e 'class A; end; puts A.new(1)'
Using /Users/user/.rvm/gems/ruby-1.9.2-p320
#<A:0x007fcdc204fac8>
➜ ~ rvm use 1.9.3 && ruby -e 'class A; end; puts A.new(1)'
Using /Users/user/.rvm/gems/ruby-1.9.3-p327
-e:1:in `initialize': wrong number of arguments(1 for 0) (ArgumentError)
from -e:1:in `new'
from -e:1:in `<main>'
module AwesomeResource
def initialize(attributes = {})
attributes.each do |key, val|
reader = key
writer = "#{key}="
unless respond_to?(reader)
self.class.send(:attr_reader, key)
end
@DNNX
DNNX / out.txt
Last active December 21, 2015 03:39
ruby crash
➜ ~ ruby -e "def f(*xs); end; f(*(1..130_000))"
-e:1: [BUG] Bus Error
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.2.0]
-- Crash Report log information --------------------------------------------
See Crash Report log file under the one of following:
* ~/Library/Logs/CrashReporter
* /Library/Logs/CrashReporter
* ~/Library/Logs/DiagnosticReports
* /Library/Logs/DiagnosticReports
@DNNX
DNNX / README.md
Last active November 19, 2015 21:19
Maybe yes, Maybe no

This is another wrong, incomplete implementation of Maybe data type in Ruby. This gist is a full-blown gem. In order to use it, just add the following line to your Gemfile.

gem 'maybe', gist: 'da2aaafdfd71e6ff0ffd'

But WHY?

To my knowledge, all existing gems are too fat and do not provide the ability to configure what values should be treated as Just and what values are Nothing. Some treat [] as Nothing, some treat only nil as Nothing, some rely on truthiness (this is the most reasonable choice in my opinion). In my particular use case, I needed to treat obj as Nothing iff obj.present? is true. Obviously, hard-coding this Railsism into the gem is not an option, so I pass the Nothingness predicate into Maybe data constructor (pardon my French).