Skip to content

Instantly share code, notes, and snippets.

Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10
@fj
fj / nano.rb
Created February 26, 2010 23:09
class Nanomachine
class Nanoparticle
def add(p)
while more?
p << self
end
end
end
# Ruby 1.9 includes nicer support for warnings that you didn't get in 1.8.
# Given a constant's name of the form r = "One::Two::[...]::ThirtyFive",
# resolve its value.
sep = "::" # Doesn't appear that '::' is defined.
c = r.split(sep).inject(Object) { |memo, el| memo.const_get(el) }
# Usage:
module Foo
module Bar
Baz = 100
end

I'm trying to be diligent about checking my rake tasks with RSpec tests, but in the process of feeling my way around I seem to have hit a wall. I've got a really simple RSpec test that looks like this:

# ./test/meta_spec.rb
describe "Rake tasks" do
  require 'rake'
  
  before(:each) do
    @rake = Rake::Application.new
    @rake.load_rakefile  # => Error here!

Rake.application = @rake

We couldn’t find that file to show.
// Given an interface (IFactory<> in this example), find everything that
// implements the interface and put it in a map.
//
// Usage:
// public interface IFactory<T> { ... }
// public class AppleFactory : IFactory<Apple> { ... }
// public class BananaFactory : IFactory<Banana> { ... }
// public class ChocolateFactory : IFactory<Chocolate> { ... }
//
// var m = new FactoryManager(); // Automatically scans this assembly for suitable implementations
require 'tempfile'
t = Tempfile.new('test-data')
t.open
t.sync = true
t << "apples"
t.puts "bananas"
# prints "contents are [] (14 bytes)"
puts "contents are [#{t.read}] (#{t.size} bytes)
[johnf@genesis][2010/06/12|17:53:00]
[/tmp]> mkdir foo
[johnf@genesis][2010/06/12|17:53:36]
[/tmp]> cd foo
[johnf@genesis][2010/06/12|17:53:39]
[/tmp/foo]> git init
Initialized empty Git repository in /tmp/foo/.git/
[johnf@genesis][2010/06/12|17:53:40]
@fj
fj / gist:436706
Created June 13, 2010 14:36
Patch for misleading #rvm message when using 'system' instead of '--system'.
diff --git a/scripts/cli b/scripts/cli
index b4cd70e..496d322 100644
--- a/scripts/cli
+++ b/scripts/cli
@@ -280,6 +280,7 @@ __rvm_parse_args() {
;;
system|default)
+ echo "Reverting to predefined interpreter '${rvm_token}'."
rvm_action=${rvm_action:-use}
@fj
fj / gist:436763
Created June 13, 2010 15:50
Helping someone who asked in #ruby about recursively replacing Array elements.
b = lambda { |e| e.kind_of?(Array) ? e.map(&b) : (e.nil? ? "" : e) }
# => #<Proc:0x0000000104d908@(irb):1 (lambda)>
arr = [5, 6, 7, nil, [nil], [6, 8, nil], [[194, 48, nil], [nil]]]
# => [5, 6, 7, nil, [nil], [6, 8, nil], [[194, 48, nil], [nil]]]
arr.map(&b)
# => [5, 6, 7, "", [""], [6, 8, ""], [[194, 48, ""], [""]]]