Skip to content

Instantly share code, notes, and snippets.

@cldwalker
cldwalker / .riplrc
Created April 24, 2011 20:22
ripl multi-line history plugin - two ways to easily access last edited code block
# Add to ~/.riplrc
module Ripl::MultiLineHistory
attr_reader :last_buffer
# hacks multi-line's loop_once
def loop_once
@cldwalker
cldwalker / example use
Created January 19, 2011 01:59
using sparql_client and hirb
$ ruby sparql_client_example.rb
+--------------------------------------------------------------------+-------------------------------------------------+-------------------------------------+
| s | p | o |
+--------------------------------------------------------------------+-------------------------------------------------+-------------------------------------+
| http://dbpedia.org/resource/Elizabeth_Peabody__Teacher | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://www.w3.org/2002/07/owl#Thing |
| http://dbpedia.org/resource/Bonython_Hall | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://www.w3.org/2002/07/owl#Thing |
| http://dbpedia.org/resource/Zemfira | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://www.w3.org/2002/07/owl#Thing |
| http://dbpedia.org/resource/Myles_Ke
@cldwalker
cldwalker / ripl-play-example
Created December 2, 2010 14:03
ripl-play example
a = 10 ** 2
a + 10
@cldwalker
cldwalker / dumped
Created June 1, 2010 20:23
Example output of boson dump_rdf command
$ boson dump_rdf http://datagraph.org/jhacker/foaf.nt
+------------------------------------+-------------------------------------------------+---------------------------------------------------+
| subject | predicate | object |
+------------------------------------+-------------------------------------------------+---------------------------------------------------+
| http://datagraph.org/jhacker/foaf | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://xmlns.com/foaf/0.1/PersonalProfileDocument |
| http://datagraph.org/jhacker/foaf | http://xmlns.com/foaf/0.1/maker | http://datagraph.org/jhacker/#self |
| http://datagraph.org/jhacker/foaf | http://xmlns.com/foaf/0.1/primaryTopic | http://datagraph.org/jhacker/#self |
| http://datagraph.org/jhacker/#self | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://xmlns.com/foaf/0.1/Person
@cldwalker
cldwalker / gemspec_builder.rb
Created May 10, 2010 20:01
Builds gemspecs for multiple gems based on a YAML config.
# GemspecBuilder builds a Gem::Specification object by looking up a gem's config in the config file ~/.gems.yml.
# GemspecBuilder assumes the current directory's basename is the gem name (unless told otherwise) and needs to be
# run inside the gem's root directory in order for the gemspec to build correctly.
#
# The builder merges the default gemspec hash in config[:default] with any gem-specific configuration in config[:gems][gem_name].
# Gemspec hashes are assumed to have the same attribute-value pairs as Gem::Specification objects (described here:
# http://rubygems.rubyforge.org/rdoc/Gem/Specification.html). The only difference is the :files attribute which takes globbed expressions
# and is converted with Dir.glob.
#
# For an example config see mine: http://github.com/cldwalker/dotfiles/blob/master/.gems.yml
@cldwalker
cldwalker / my_core.rb
Created January 29, 2010 20:14
Example of overriding a boson core command in a separate library
module MyCore
def self.config
{:force=>true}
end
# My overridden version that also handles arrays of hashes with :url
def browser(*urls)
urls.map! {|e| e[:url] } if urls[0].is_a?(Hash) and urls[0].has_key?(:url)
system('open', *urls)
end
@cldwalker
cldwalker / run_example.rb
Created December 28, 2009 22:37 — forked from jeroenvandijk/run_example.rb
Example of using Boson to call subcommands in one file
# In example.rb
module Example
extend self
# This method does something
def command1(arg1, arg2, options = {})
end
# This method does something else
def command2(arg1, arg2, options = {})
@cldwalker
cldwalker / chars.rb
Created December 18, 2009 09:10 — forked from jeroenvandijk/chars.rb
Example of using Boson as an option parser
#!/usr/bin/env ruby
module Chars
extend self
def scale(args, options={})
scale = options[:scale]
chars = args.map { |x| Chars.char(x.to_i).map { |row| row.split("") } }
print_chars scale_vertically(scale_horizontally(chars, scale), scale)
end
@cldwalker
cldwalker / my_active_record_table.rb
Created September 3, 2009 06:44
Example of customizing hirb helper for active record
# Customize description to include model class
class Hirb::Helpers::MyActiveRecordTable < Hirb::Helpers::AutoTable
def self.render(rows, options={})
@model_class = rows.is_a?(Array) ? rows[0].class : rows.class
super
end
def self.model_class; @model_class; end
def render_table_description
@cldwalker
cldwalker / mini-console.rb
Created July 23, 2009 11:34
mini script/console
['readline', 'rubygems', 'bond', File.dirname(__FILE__) + '/../config/boot'].each {|e| require e }
Bond.start
["#{RAILS_ROOT}/config/environment", 'console_app', 'console_with_helpers'].each {|e| require e}
history_file = File.join(ENV["HOME"], '.myrb_history')
IO.readlines(history_file).each {|e| Readline::HISTORY << e.chomp } if File.exists?(history_file)
while (input = Readline.readline('>> ', true)) != 'exit'
begin puts "=> #{eval(input).inspect}"; rescue Exception; puts "Error: #{$!}" end
end
File.open(history_file, 'w') {|f| f.write Readline::HISTORY.to_a.join("\n") }