Permalink
Browse files

First

  • Loading branch information...
0 parents commit 2f89f05077dba070681df32164731016fb929b1d @jsl committed Aug 4, 2012
@@ -0,0 +1,2 @@
+*.class
+*.jar
@@ -0,0 +1,30 @@
+# Running Ruby from Clojure
+
+This is a simple example of calling Ruby from Clojure. It contains a simple
+JRuby project and a Clojure project:
+
+* steel_city - a Ruby project that returns the speakers for the given day
+ in the Steel City Ruby Conference 2012
+* speaker_selector - Returns a "top" speaker for the given day using a
+ sophisticated algorithm (ie, random selection)
+
+## Compiling
+
+ cd steel_city
+ jrubyc --javac schedule.rb
+ jar -cf schedule.jar com/steelcity/Schedule.class
+
+## Installing with mvn
+
+In your clojure project, set up the dependencies in the project.clj.
+You will need dependencies on jruby and your compiled jar.
+
+You will have to install these with mvn so that lein deps loads the
+dependencies:
+
+ mvn install:install-file -DgroupId=com.steelcity.schedule \
+ -DartifactId=com.steelcity.schedule -Dversion=0.0.0 \
+ -Dpackaging=jar -Dfile=schedule.jar
+
+The command to install jruby to your local mvn repository will be similar.
+
@@ -0,0 +1,8 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
@@ -0,0 +1,13 @@
+# speaker_selector
+
+Selects a random speaker
+
+## Usage
+
+
+
+## License
+
+Copyright (C) 2012 Chris Hunt and Justin Leitgeb
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,5 @@
+(defproject speaker_selector "1.0.0-SNAPSHOT"
+ :description "Selects a top speaker from the schedule"
+ :dependencies [[org.clojure/clojure "1.3.0"]
+ [jruby "1.6.7.2"]
+ [com.steelcity.schedule "0.0.0"]])
@@ -0,0 +1,7 @@
+(ns speaker-selector.core
+ (import [com.steelcity Schedule]))
+
+(defn top-speaker
+ "Returns the top speaker for the given day"
+ [day]
+ (first (shuffle (.speakers (new Schedule day)))))
@@ -0,0 +1,6 @@
+(ns speaker_selector.test.core
+ (:use [speaker_selector.core])
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))
@@ -0,0 +1,87 @@
+package com.steelcity;
+
+import org.jruby.Ruby;
+import org.jruby.RubyObject;
+import org.jruby.javasupport.util.RuntimeHelpers;
+import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.javasupport.JavaUtil;
+import org.jruby.RubyClass;
+
+
+public class Schedule extends RubyObject {
+ private static final Ruby __ruby__ = Ruby.getGlobalRuntime();
+ private static final RubyClass __metaclass__;
+
+ static {
+ String source = new StringBuilder("# -*- coding: utf-8 -*-\n" +
+ "require 'java'\n" +
+ "java_package 'com.steelcity'\n" +
+ "\n" +
+ "class Schedule\n" +
+ " attr_reader :day\n" +
+ "\n" +
+ " PRESENTERS = {\n" +
+ " 'Friday' => ['Corey Haines', 'Josh Susser', 'Andrew Clay Shafer', \n" +
+ " 'Steve Klabnik & Lindsey Bieda', 'Mattt Thompson', \n" +
+ " 'Jeff Casimir'],\n" +
+ " 'Saturday' => ['Aaron Patterson', 'Jennifer Payne', 'Jim Weirich', \n" +
+ " 'Joe O’Brien'] \n" +
+ " }\n" +
+ "\n" +
+ " def initialize(day)\n" +
+ " raise \"Invalid day\" unless [\"Friday\", \"Saturday\"].include?(day)\n" +
+ "\n" +
+ " @day = day\n" +
+ " end\n" +
+ "\n" +
+ " def speakers\n" +
+ " PRESENTERS[day]\n" +
+ " end\n" +
+ "end\n" +
+ "").toString();
+ __ruby__.executeScript(source, "schedule.rb");
+ RubyClass metaclass = __ruby__.getClass("Schedule");
+ metaclass.setRubyStaticAllocator(Schedule.class);
+ if (metaclass == null) throw new NoClassDefFoundError("Could not load Ruby class: Schedule");
+ __metaclass__ = metaclass;
+ }
+
+ /**
+ * Standard Ruby object constructor, for construction-from-Ruby purposes.
+ * Generally not for user consumption.
+ *
+ * @param ruby The JRuby instance this object will belong to
+ * @param metaclass The RubyClass representing the Ruby class of this object
+ */
+ private Schedule(Ruby ruby, RubyClass metaclass) {
+ super(ruby, metaclass);
+ }
+
+ /**
+ * A static method used by JRuby for allocating instances of this object
+ * from Ruby. Generally not for user comsumption.
+ *
+ * @param ruby The JRuby instance this object will belong to
+ * @param metaclass The RubyClass representing the Ruby class of this object
+ */
+ public static IRubyObject __allocate__(Ruby ruby, RubyClass metaClass) {
+ return new Schedule(ruby, metaClass);
+ }
+
+
+ public Schedule(Object day) {
+ this(__ruby__, __metaclass__);
+ IRubyObject ruby_day = JavaUtil.convertJavaToRuby(__ruby__, day);
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "initialize", ruby_day);
+
+ }
+
+
+ public Object speakers() {
+
+ IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "speakers");
+ return (Object)ruby_result.toJava(Object.class);
+
+ }
+
+}
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+require 'java'
+java_package 'com.steelcity'
+
+class Schedule
+ attr_reader :day
+
+ PRESENTERS = {
+ 'Friday' => ['Corey Haines', 'Josh Susser', 'Andrew Clay Shafer',
+ 'Steve Klabnik & Lindsey Bieda', 'Mattt Thompson',
+ 'Jeff Casimir'],
+ 'Saturday' => ['Aaron Patterson', 'Jennifer Payne', 'Jim Weirich',
+ 'Joe O’Brien']
+ }
+
+ def initialize(day)
+ raise "Invalid day" unless ["Friday", "Saturday"].include?(day)
+
+ @day = day
+ end
+
+ def speakers
+ PRESENTERS[day]
+ end
+end

0 comments on commit 2f89f05

Please sign in to comment.