Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
keithrbennett / reverse_encrypter.rb
Created June 19, 2011 21:32
ReverseEncrypter - Naive Implementation of EncryptionContract
require 'encryption_contract'
# Naive and totally lame encrypter that just reverses the string.
class ReverseEncrypter
include EncryptionContract
def encrypt(s)
s.reverse
end
@keithrbennett
keithrbennett / sample.kml
Created June 24, 2011 18:08
Sample KML file
<?xml version="1.0"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<Placemark>
<name>Reston, Va</name>
<description>Anchor_Name: Reston, Va&lt;br/&gt; Canonical Name: &lt;br/&gt;Country Name: US&lt;br/&gt;Population: 6829&lt;br/&gt;</description>
<Point>
<coordinates>17,47</coordinates>
</Point>
</Placemark>
@keithrbennett
keithrbennett / parse_recursive.rb
Created October 20, 2011 02:08
Recursive parse that takes a flat array and returns an array of arrays of a max size.
def parse(input_array, max_elements, output_array = [])
(input_array && (! input_array.empty?)) \
? parse(input_array[max_elements..-1], max_elements, output_array << input_array[0...max_elements]) \
: output_array
end
@keithrbennett
keithrbennett / example_output.txt
Created November 30, 2011 05:22
Example Output
Success : Yes
Total Resistance : 16
Row Numbers : 1, 2, 3, 4, 4, 5
.
Success : Yes
Total Resistance : 11
Row Numbers : 1, 2, 1, 5, 5, 5
.
@keithrbennett
keithrbennett / FrameInRuby.dump
Created December 19, 2011 21:28
Decompiled FrameInRuby.class (using JD-GUI from http://java.decompiler.free.fr/?q=jdgui)
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyFixnum;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.anno.JRubyMethod;
import org.jruby.ast.executable.AbstractScript;
import org.jruby.ast.executable.RuntimeCache;
import org.jruby.internal.runtime.methods.CallConfiguration;
import org.jruby.javasupport.util.RuntimeHelpers;
=begin
Reference: $100,000 from table, single status -> $21,330, $78,670
Income: 100000.00 Tax: 21617.00 After Tax: 78383.00
Income: 100010.00 Tax: 21619.80 After Tax: 78390.20
Income: 100020.00 Tax: 21622.60 After Tax: 78397.40
Income: 100030.00 Tax: 21625.40 After Tax: 78404.60
Income: 100040.00 Tax: 21628.20 After Tax: 78411.80
Income: 100050.00 Tax: 21631.00 After Tax: 78419.00
@keithrbennett
keithrbennett / query_to_hash.rb
Created August 17, 2012 02:23
Solution to query_to_hash problem.
# This file is an rspec runnable file.
# (I didn't name it with _spec because its primary purpose is to provide
# the method, not run the test.)
# Here's the function that solves the problem, and the require needed to run it.
require 'uri'
def query_to_hash(query)
@keithrbennett
keithrbennett / jruby-include-question.rb
Created August 18, 2012 00:17
Shows a JRuby implementation of a Java interface that surprisingly works with 'include' statement.
# Shouldn't this fail with the 'include Runnable' disabled?
require 'java'
java_import 'java.lang.Runnable'
class SampleRunnable
# include Runnable
@keithrbennett
keithrbennett / jruby-2457.rb
Created August 18, 2012 22:48
Illustrates problem instantiating Ruby subclass of Java class w/different constructor arities.
require 'java'
java_import 'java.lang.Runnable'
class SampleRunnable
include Runnable
def initialize(greeting)
@greeting = greeting
@keithrbennett
keithrbennett / mail-using-gmail.rb
Created September 3, 2012 00:47
Mail Script from Keith Bennett's Technical Blog
#!/usr/bin/env ruby
# Mails a file using GMail's SMTP Server.
# For illustrative purposes; error checking and testing intentionally omitted for brevity.
#
# Requirements:
# 1) the 'mail' gem must be installed
# 2) a file named 'pw.txt' containing the Google password must be present
# in the current directory.
#
# Ruby versions: tested on 1.9.3, 1.8.7, JRuby