From cee1278d14a3d41a2f49c3454b648616fe0cecb1 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sun, 26 Jun 2011 20:05:54 -0700 Subject: [PATCH] Fix Ruby 1.9.2 support. --- ampex.gemspec | 2 +- lib/ampex.rb | 28 +++++++++++++++++++++++----- spec/ampex_spec.rb | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ampex.gemspec b/ampex.gemspec index 7d83dc3..11eb511 100644 --- a/ampex.gemspec +++ b/ampex.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = "ampex" - s.version = "1.1.2" + s.version = "1.2.0" s.platform = Gem::Platform::RUBY s.author = "Conrad Irwin" s.email = "conrad.irwin@gmail.com" diff --git a/lib/ampex.rb b/lib/ampex.rb index 0267b86..45af11c 100644 --- a/lib/ampex.rb +++ b/lib/ampex.rb @@ -1,10 +1,22 @@ -require 'blankslate' - # Copyright 2010 Conrad Irwin MIT License # # For detailed usage notes, please see README.markdown -# -class Metavariable < BlankSlate + +# NOTE: Ruby 1.9 seems to provide a default blank slate that isn't +# very blank, luckily it also provides a BasicObject which is pretty +# basic. +if defined? BasicObject + superclass = BasicObject +else + require 'rubygems' + require 'blankslate' + superclass = BlankSlate +end + +class Metavariable < superclass + # Take a local copy of these as constant lookup is destroyed by BasicObject. + Metavariable = self + Thread = ::Thread # When you pass an argument with & in ruby, you're actually calling #to_proc # on the object. So it's Symbol#to_proc that makes the &:to_s trick work, @@ -12,7 +24,7 @@ class Metavariable < BlankSlate attr_reader :to_proc def initialize(&block) - @to_proc = block || lambda{|x| x} + @to_proc = block || ::Proc.new{|x| x} end # Each time a method is called on a Metavariable, we want to create a new @@ -32,6 +44,12 @@ def method_missing(name, *args, &block) mv end + # BlankSlate and BasicObject have different sets of methods that you don't want. + # let's remove them all. + instance_methods.each do |method| + undef_method method unless %w(method_missing to_proc __send__ __id__).include? method.to_s + end + private # In order to support assignment via &X (expressions of the form &X['one'] = 2), diff --git a/spec/ampex_spec.rb b/spec/ampex_spec.rb index 881be5a..f8d8813 100644 --- a/spec/ampex_spec.rb +++ b/spec/ampex_spec.rb @@ -63,7 +63,7 @@ def intercept(b) it "should preserve existing #to_proc in an object's singleton class" do a = Object.new class << a - def to_proc; lambda { 3 }; end + def to_proc; lambda { |x| 3 }; end end [1].map(&a).should == [3]