Skip to content

Instantly share code, notes, and snippets.

# 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
@keithrbennett
keithrbennett / Bug.java
Created February 11, 2013 05:49
Dave Medinets' Algorithm Example
package com.affy;
public class Bug {
private int[] id;
public Bug(int n) {
id = new int[n];
for (int i = 0; i < n; i++) {
id[i] = i;
@keithrbennett
keithrbennett / gist:4956124
Created February 14, 2013 20:30
Trying to use Chef omnibus installer on Linux Mint 14 32-bit VM...
user@linuxmint-14-32bit ~ $ uname -a
Linux linuxmint-14-32bit 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012 i686 i686 i686 GNU/Linux
user@linuxmint-14-32bit ~ $ curl -L https://www.opscode.com/chef/install.sh | sudo bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6471 100 6471 0 0 17436 0 --:--:-- --:--:-- --:--:-- 36150
Downloading Chef for linuxmint...
Unable to retrieve a valid package!
@keithrbennett
keithrbennett / thread_example.rb
Last active December 14, 2015 04:09
Ruby script for comparing multithreaded operation in Ruby implementations (eg. Ruby vs. JRuby).
# Simple Ruby/JRuby program to illustrate that MRI Ruby uses only one CPU,
# while JRuby will use all available CPU's. (Inspection tool not included;
# you can use Activity Monitor on a Mac, or htop on many Unix distros.
#
# Keith R. Bennett, @keithrbennett
NUM_THREADS = 5
SLEEP_DURATION_IN_SEC = 2
@keithrbennett
keithrbennett / locale_displayer.rb
Last active December 14, 2015 04:09
Example JRuby program to display the JVM's available locales and their number, currency, and date formats. Change the format constants to see more kinds of formatting (e.g. DateFormat::LONG/SHORT). - Keith R. Bennett, @keithrbennett
# Example JRuby program to display the JVM's available locales
# and their number, currency, and date formats.
#
# Keith R. Bennett, @keithrbennett
require 'java'
java_import 'java.util.Locale'
java_import 'java.text.DateFormat'
java_import 'java.text.NumberFormat'
➜ ruby-validator git:(master) ✗ rspec x_spec.rb
/Users/kbennett/work/small-projects/ruby-validator/x_spec.rb:2:in `require': cannot load such file -- logging (LoadError)
from /Users/kbennett/work/small-projects/ruby-validator/x_spec.rb:2:in `<top (required)>'
from /Users/kbennett/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /Users/kbennett/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/kbennett/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/kbennett/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/kbennett/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/kbennett/.rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /Users/kbennett/.rvm/gems/ruby-1.
@keithrbennett
keithrbennett / gist:5570290
Created May 13, 2013 18:20
Output of JRuby RSpec test, after tests all pass. I'm calling a networking library during the tests, but don't know if that is connected with this problem.
Exception in thread "RubyThread-14: /Users/kbennett/.rvm/gems/jruby-1.7.3/gems/logging-1.8.1/lib/logging/diagnostic_context.rb:320" java.nio.channels.CancelledKeyException
at sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:55)
at sun.nio.ch.SelectionKeyImpl.readyOps(SelectionKeyImpl.java:69)
at sun.nio.ch.KQueueSelectorImpl.updateSelectedKeys(KQueueSelectorImpl.java:105)
at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:74)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:69)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:80)
at org.jruby.util.io.SelectBlob.doSelect(SelectBlob.java:257)
at org.jruby.util.io.SelectBlob.goForIt(SelectBlob.java:89)
at org.jruby.RubyIO.select_static(RubyIO.java:3507)
@keithrbennett
keithrbennett / logging_demo.rb
Created June 3, 2013 01:19
Demonstrates the use of the Ruby 'logging' gem.
#!/usr/bin/env ruby
require 'facter'
require 'logging'
require 'pry'
require 'yaml'
GLOBAL_LOGGING = true
@keithrbennett
keithrbennett / build_gem.sh
Created June 13, 2013 02:56
Shows a strategy for creating both MRI and JRuby versions of a gem.
# Builds the gem for the current rvm ruby/platform.
# Run this once for MRI and once for JRuby, as in:
#
# rvm 1.9
# ./build_gem.sh
# rvm jruby
# ./build_gem.sh
GEM_NAME=my_gem
class Name
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def name