Skip to content

Instantly share code, notes, and snippets.

@sam
sam / gist:4162035
Created November 28, 2012 15:37
Symbol.all_symbols behavior
irb(main):010:0> Symbol.all_symbols.map(&:to_s).grep /all_symbols/
=> []
irb(main):011:0> Symbol.all_symbols.map(&:to_s).grep /foocowbar/
=> []
irb(main):012:0> def foocowbar; nil; end
=> nil
irb(main):013:0> Symbol.all_symbols.map(&:to_s).grep /foocowbar/
=> ["foocowbar"]
@sam
sam / gist:4119560
Created November 20, 2012 17:48
Lambda syntax guidelines
  • The new lambda literal syntax is preferred in Ruby 1.9.

    # BAD
    lambda = lambda { |a, b| a + b }
    
    # GOOD
    lambda = ->(a, b) { a + b }
@sam
sam / gist:4113718
Created November 19, 2012 20:36
QBE example
module Query
class Expression
attr_reader :field, :operator, :target
def initialize(field, operator, target)
@field, @operator, @target = field, operator, target
end
end
@sam
sam / compiler.rb
Created October 23, 2012 14:37
Doubleshot test output (for run all specs, triggered by Signal.trap("INT")
# This comes from here: https://github.com/sam/doubleshot/blob/master/lib/doubleshot/compiler.rb
#
# If you have JRuby 1.7.0, Java7, Maven and Bundler installed, you can reproduce the attached output like so:
#
# cd ~/src
# git clone git://github.com/sam/doubleshot.git
# cd doubleshot
# bundle install
# bin/doubleshot test --ci
@sam
sam / assertion_style_spec.rb
Created October 4, 2012 15:18
new lambda syntax + must_raise
it "must raise an exception if the file does not exist" do
Helper::tmp do |tmp|
assert_raises(Errno::ENOENT) do
Doubleshot::Compiler::Classpath.new.add(tmp + "asdf")
end
end
end
@sam
sam / Bar.java
Created September 14, 2012 17:22
Ruby JSON vs Java Jackson
package org.foo;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.io.IOException;
public class Bar {
static final ObjectMapper mapper = new ObjectMapper();
@sam
sam / Bar.java
Created September 12, 2012 17:10
Simplest possible JRuby+Java test
package org.foo;
public class Bar {
public static int baz() {
return 1;
}
}
@sam
sam / forms_person.html.erb
Created September 4, 2012 16:05
First Class Forms API notes
<% # NOTE: The actual path is: views/forms/person.html.erb %>
<% # Or we can override the default rendering like so: %>
<h2>People Form</h2>
<% # By default the form-helper will POST to the current location %>
<% form do |form| %>
<table>
<tr><td>First Name:</td><td><%= form.field :first_name %></td></tr>
<tr><td>Last Name:</td><td><%= form.field :last_name %></td></tr>
@sam
sam / bench.rb
Created August 29, 2012 20:41
ActiveSupport HashWithIndifferentAccess vs Java HashMap performance
#!/usr/bin/env jruby
require "rubygems"
require "active_support/hash_with_indifferent_access"
require "java"
require "benchmark"
SYMBOLS = []
STRINGS = []
GET_REPEAT = 100
# I get an HTTP 405: HTTP method GET is not supported by this URL
java_import javax.servlet.http.HttpServlet
class HelloWorld < HttpServlet
def process(request, response)
response.writer.println "Hello World!"
end
end