Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
keithrbennett / gist:3713110
Created September 13, 2012 09:15
Diff from using Forwardable to replace delegating methods
diff --git a/lib/life_game_viewer/view/life_table_model.rb b/lib/life_game_viewer/view/life_table_model.rb
index 0ee2966..6cbcba1 100644
--- a/lib/life_game_viewer/view/life_table_model.rb
+++ b/lib/life_game_viewer/view/life_table_model.rb
@@ -3,15 +3,26 @@ require 'java'
java_import javax.swing.table.AbstractTableModel
java_import javax.swing.JOptionPane
+require 'forwardable'
+
@keithrbennett
keithrbennett / rspec-rails-new.rb
Created October 18, 2012 22:23
Peforms all steps necessary to create a Rails project that will use RSpec instead of TestUnit
#!/usr/bin/env ruby
# Creates a new Rails project configured to use
# rspec instead of TestUnit.
#
# - Keith Bennett, Github/Twitter: keithrbennett
ADD_RSPEC_COMMAND = %{
group :development, :test do
@keithrbennett
keithrbennett / rspec-rails-builder.rb
Created October 19, 2012 01:19
A Rails builder that will create projects using RSpec instead of TestUnit.
# A Rails builder that will create projects using RSpec instead of TestUnit.
# Invoke as: rails new my_project builder=rspec-rails-builder.rb
class AppBuilder < Rails::AppBuilder
# Override superclass test method to do rspec stuff.
def test
puts('Adding rspec to Gemfile.')
append_rspec_to_gemfile
@keithrbennett
keithrbennett / rails-composer-output.txt
Created October 19, 2012 03:30
Output of Rails Composer, including errors
>rails new myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb -T
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/images/rails.png
@keithrbennett
keithrbennett / find_jruby.rb
Created October 23, 2012 23:01
For poster on JRuby forum to find JRuby in his Windows path
def list_candidates(extensions, command_name)
dirs = ENV['PATH'].split(File::PATH_SEPARATOR)
filespecs = []
dirs.each do |dir|
filespec_stem = File.join(dir, command_name)
extensions.each do |extension|
filespecs << (filespec_stem + extension)
end
end
@keithrbennett
keithrbennett / ruby-fp.rb
Created November 5, 2012 00:24
Functional Programming Playground With Ruby
#!/usr/bin/env ruby
LINE_SEPARATOR = '-' * 79
# Currying - using a function that takes n parameters,
# create a new function that call it, providing some of those
# parameters so that it requires < n parameters
# (e.g. double for mult, square for power below):
@keithrbennett
keithrbennett / class_vars.rb
Created November 6, 2012 06:43
Class Variables vs. Class Instance Variables
=begin
The "Class Variable" Method in class GuidProvider1 (the "@@" notation) is
simpler to use, but violates the principle that the object that owns a
variable is the only one who can directly read or modify it. That is, there
is no privacy or protection, in that there is no way the instances can be
prevented from reading and writing to the variable (for example, the 'next'
instance method does both).
In contrast, the "Class Instance Variable" single @ notation as shown in
@keithrbennett
keithrbennett / gist:4290991
Created December 15, 2012 03:01
Function to calculate prime numbers...uses !!!.
def prime_numbers(upper_bound)
return [] if upper_bound < 2
primes = [2]
3.upto(upper_bound) do |n|
is_prime = !!! primes.detect { |prime| mult_of?(n, prime) }
primes << n if is_prime
end
return primes
end
@keithrbennett
keithrbennett / jruby-resolv.txt
Created January 5, 2013 05:28
Illustrates problems I'm having using Resolv.getaddress with JRuby.
# JRuby 1.7.1 on Mac OS X, java version "1.6.0_37"
>rvm 1.9
>ruby -e "require 'resolv'; puts Resolv.getaddress 'cnn.com'"
157.166.255.19
>rvm jruby # 1.7.1
>ruby -e "require 'resolv'; puts Resolv.getaddress 'cnn.com'"
Resolv::ResolvError: no address for cnn.com
getaddress at /Users/keithb/.rvm/rubies/jruby-1.7.1/lib/ruby/1.9/resolv.rb:93
@keithrbennett
keithrbennett / jruby_java_max_benchmark.rb
Last active December 19, 2017 06:03
Shows the performance effects of crossing the JRuby <--> Java bridge, and using JRuby's java_method.
# Illustrates the overhead of the JRuby <--> Java bridge
# parameter and return value type conversions, and the
# dramatic performance improvement that can be had by
# specifying the Java overload using java_method.
require 'java'
require 'benchmark'
ITERATION_COUNT = ARGV.empty? ? 100_000 : ARGV.first.to_i
JMath = java.lang.Math