Skip to content

Instantly share code, notes, and snippets.

View bhuga's full-sized avatar

Ben Lavender bhuga

View GitHub Profile
@bhuga
bhuga / client-assert.rb
Created February 19, 2011 22:08
Demonstrates assertions, server
#!/usr/bin/env ruby
require 'ffi-rzmq'
#IPC_SOCKET = "ipc:///tmp/assert-demo.zmq"
IPC_SOCKET = 'tcp://localhost:5555'
@sender_context = ZMQ::Context.new(2)
send_socket = @sender_context.socket(ZMQ::REQ)
second_send_socket = nil
send_socket.connect(IPC_SOCKET)
@bhuga
bhuga / sparql-client-refactor.rb
Created February 1, 2011 18:33
Updates to SPARQL client to allow parsing with other tools, if so wished.
require 'sparql/client'
require 'rest_client'
sparql = SPARQL::Client.new("http://dbpedia.org/sparql")
# existing functionality:
p sparql.query("select distinct ?Concept where {[] a ?Concept}").inspect
# now equivalent to:
response = RestClient.post 'http://dbpedia.org/sparql', { :query => "select distinct ?Concept where {[] a ?Concept}" }, :content_type => 'application/x-www-form-urlencoded', :accept => 'application/sparql-results+json'
require 'rubygems'
require 'ruby-debug'
require 'spira'
require 'rdf/rdfxml'
require 'sparql/client'
class Actor
include Spira::Resource
@bhuga
bhuga / RDF.rb encoding error when fetching remote files
Created November 14, 2010 00:25
Local copy of the file works, fetching directly over remote fails. Something wrong with standard library?
root@ip-10-251-122-82:/home/lavender/spocq/src/test/rspec/tests/data-r2/i18n# curl http://tmp.datagraph.org.s3.amazonaws.com/import/33f361f0-d1b2-012d-f3cf-0023df827da6 > curl
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
113 795 113 795 0 0 47522 0 --:--:-- --:--:-- --:--:-- 56785
root@ip-10-251-122-82:/home/lavender/spocq/src/test/rspec/tests/data-r2/i18n# diff curl normalization-01.ttl
root@ip-10-251-122-82:/home/lavender/spocq/src/test/rspec/tests/data-r2/i18n# ls -al curl normalization-01.ttl
-rw-r--r-- 1 root root 795 2010-11-14 00:21 curl
-rw-r--r-- 1 lavender lavender 795 2010-10-29 05:46 normalization-01.ttl
root@ip-10-251-122-82:/home/lavender/spocq/src/test/rspec/tests/data-r2/i18n# irb1.9
@bhuga
bhuga / spira
Created October 18, 2010 10:58
first draft. Needs implementation of @setup, @teardown, @query.
#!/usr/bin/env ruby1.9
# local spira bug fix for bnode objects with Types::Any
$:.unshift '~/repos/spira/lib'
require 'spira'
require 'rdf/n3'
require 'rdf/isomorphic'
DAWG = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#')
MF = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#')
QT = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-query#')
ben:promising-future ben$ rake bench; rake1.9 bench; jrake bench
(in /Users/ben/Repos/promising-future)
Rehearsal ----------------------------------------------------------
Nothing 0.000000 0.000000 0.000000 ( 0.004472)
Nothing(Promise) 0.010000 0.000000 0.010000 ( 0.009008)
Nothing(Future) 0.120000 0.050000 0.170000 ( 0.168207)
A method 0.000000 0.000000 0.000000 ( 0.005997)
A method(Promise) 0.010000 0.000000 0.010000 ( 0.012526)
A method(Future) 0.130000 0.040000 0.170000 ( 0.171445)
#== 0.000000 0.000000 0.000000 ( 0.004996)
I hacked up methodmissing's promise to rename it to ExtPromise. Then I edited the benchmark program like so:
require "benchmark"
require "ext/promise/promise"
require "promise"
TESTS = 1000
Benchmark.bmbm do |results|
# Picked inspect instead of undefined, as the pure-ruby promise will raise an
# exception on undefined instead of return the promise value
class ThreadTest
def initialize
# kicks off the thread, and only afterwards assigns thread to @t
@t = Thread.new { joiner }
end
def joiner
# without this sleep, will sometimes crash,
# as @t is not yet the thread, so will try to call @t.join on nil
#!/usr/bin/env ruby1.9
require 'rdf'
require 'rdf/rdfxml'
require 'rdf/ntriples'
require 'rdf/isomorphic'
s1 = %(<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
require 'promise'
four = promise { 2 + 2 }
puts "respond_to? :odd? #{four.respond_to?(:odd?)}"
# => true
puts "respond_to? :asdfsa? #{four.respond_to?(:asdfasdf?)}"
# => false