Skip to content

Instantly share code, notes, and snippets.

class ScreenshotFormatter < Cucumber::Formatter::Pretty # Cucumber::Ast::Visitor
def self.browser=(browser)
@@browser = browser
end
def self.screenshot_path=(path)
@@sspath = path
end
# This is a snippet from my ~/.irbrc
# You need to gem install ruby2ruby ParseTree
# This is what you do with it
#
# > class Bar; define_method(:foo) { 'ou hai' }; end
# > puts Bar.to_ruby
# class Bar < Object
# def foo
# "ou hai"
# Always wanted to chain comparisons the snaky way?
#
# puts 'Right there!' if 5 < x <= 15
#
# There you go.
# May be used under the terms of the MIT License
# Contributors: Tero Tilus <tero@tilus.net>, Jan Steffens
class FalseClass
@terotil
terotil / gol.rb
Created August 17, 2011 23:35
Conway's Game of Life
#!/usr/bin/ruby
seed = <<eos
..........
..........
..#.......
...#......
...##.....
...##.....
....#.....
@terotil
terotil / foo.rb
Created January 3, 2012 21:01
Cross-loading ruby classes from multiple files
puts 'foo'
class Foo
@@loaded ||= []
@@loaded << 'foo.rb'
def foo
'Foo#foo in foo'
end
@terotil
terotil / stat.rb
Created March 6, 2012 06:49
which approach is better?
class Stat
def initialize(raw)
@raw = raw
end
def file
@file ||= parse_file_from_raw
end
def name
class Repo
def initialize(path)
@path = path
end
def commits
@commits ||= log.
gsub(/\t/, " "). # remove tabs
split('$$'). # split by marker
select { |commit| commit != "" }. # only consider non-empty
@value or= do ->
1 + 2
@terotil
terotil / nda2csv.rb
Created January 10, 2013 21:03
Processor for Nordea machine readable bank statements (NDA format). Functionally minimal, just enough to outline a friendly interface and be able to export main transaction records to CSV, which is further processable to OFX using https://github.com/terotil/ofxify
require 'date'
module Nordea
module NDA
class Record
def self.parse(str)
self.new(str).normalized
end
def initialize(str)
@terotil
terotil / file_join_encoding_workaround.rb
Last active December 17, 2015 23:19
Needed to work around Ruby File.join returning ASCII-8BIT result. Dunno if aligning argument encodings was strictly necessary for my usecase, but I left it in, even though I noticed that UTF-16 and -32 won't work in any case (ArgumentError: string contains null byte). This workaround blows in your face if there is an argument that can't be encod…
# Work around https://bugs.ruby-lang.org/issues/6104
if RUBY_VERSION =~ /^1\.9/ && RUBY_REVISION < 34865
class File
class << self
def join_with_encoding_fix(*args)
target_encoding = args.first.to_s.encoding
aligned_args = args.map { |arg| arg.to_s.dup.encode!(target_encoding) }
join_without_encoding_fix(*aligned_args).force_encoding(target_encoding)
end
alias_method :join_without_encoding_fix, :join