Skip to content

Instantly share code, notes, and snippets.

@rklemme
rklemme / RoedyNumberMatch.java
Last active August 29, 2015 13:59
Test program for a specific number match.
package regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RoedyNumberMatch {
private static final String[] P_FIXES = { "", "foo", "bar ", "20" };
private static final Pattern PAT = Pattern
@rklemme
rklemme / pipes.rb
Last active September 9, 2016 14:14
Execute multiple commands connected through pipes either via threads or processes.
#!/usr/bin/ruby -w
if ENV['DEBUG']
def debug(message)
$stderr.puts message
end
else
def debug(message)
# nop
end
@rklemme
rklemme / automated_class_instance_collaboration.rb
Last active May 12, 2017 15:49
Example for adding functionality to instances of a class which needs help from class methods of the same class. Use two modules - one for instance methods and one for class methods.
#!/usr/bin/ruby -w
module Foo
module Foo4Class
def reset_instance_count
@instance_count = 0
end
def new(*a, &b)
super.tap { @instance_count += 1 }
@rklemme
rklemme / find-duplicate-id-instance.rb
Created October 25, 2013 08:57
Solution suggestion for "Help with ideas for finding dups on very large file"
#!/usr/bin/ruby -w
Entry = Struct.new :id, :instance do
def self.parse(line)
if /ID=\s*'([^']*)'\s+INSTANCE=\s*'([^']*)'/ =~ line
new $1, $2
else
raise "Cannot parse: %p" % line
end
end
@rklemme
rklemme / test-01.rb
Last active December 24, 2015 11:19
Code examples about "prior knowledge"
#!/bin/ruby
require 'benchmark'
def sin radians
h = Hash.new(Math.sin(radians))
h[0.0] = 0
h[90.0] = 1
h[180.0] = 0
h[270.0] = -1
@rklemme
rklemme / ip.data
Created June 12, 2013 07:18
Sample program to parse firewall list output
Shorewall 4.5.17.1 per-IP Accounting at fw1 - Sun Jun 9 18:38:05 EST
2013
Showing table: cowboys
IP: 192.168.200.1 SRC packets: 4196607 bytes: 292644635 DST packets: 7224498 bytes: 762829278
IP: 192.168.200.2 SRC packets: 77289 bytes: 4799573 DST packets: 324472 bytes: 481821874
IP: 192.168.200.3 SRC packets: 122875 bytes: 14531084 DST packets: 145748 bytes: 170332152
IP: 192.168.200.4 SRC packets: 142254 bytes: 18836983 DST packets: 106100 bytes: 26420351
IP: 192.168.200.5 SRC packets: 3424090 bytes: 181079976 DST packets: 6948051 bytes: 1798270528
IP: 192.168.200.6 SRC packets: 2758 bytes: 234311 DST packets: 12875 bytes: 18882957
@rklemme
rklemme / bench.out
Last active December 17, 2015 02:09
Quick benchmark of string appending
Rehearsal ------------------------------------------------------------------
interpol 0.218000 0.000000 0.218000 ( 0.218013)
plus 0.156000 0.000000 0.156000 ( 0.158009)
append 0.390000 0.000000 0.390000 ( 0.386022)
const interpol 0.203000 0.000000 0.203000 ( 0.193011)
const plus 0.218000 0.000000 0.218000 ( 0.231013)
const append 0.203000 0.000000 0.203000 ( 0.196011)
--------------------------------------------------------- total: 1.388000sec
user system total real
@rklemme
rklemme / respond_to_vs_include.rb
Last active December 15, 2015 17:49
Modified version which compares the single method check times (which need to include Mock.instance_methods(false)).
# Fancy benchmark...
# is respond_to? so faster than include?
require 'benchmark'
n = 10_000_000
mock_methods = ('a'..'z').to_a
class Mock
end
# Fancy benchmark...
# is respond_to? so faster than include?
require 'benchmark'
n = 10_000_000
mock_methods = ('a'..'z').to_a
class Mock
end
@rklemme
rklemme / matrix.rb
Last active December 12, 2015 12:19
Sample matrix with abstraction of rows and columns. This can certainly be improved, it's just to convey an idea.
#!/usr/bin/ruby
class Matrix
def initialize(row_headers, col_headers)
@row_headers = row_headers
@col_headers = col_headers
end
def [](row, col)