Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save gstark/435145 to your computer and use it in GitHub Desktop.

Select an option

Save gstark/435145 to your computer and use it in GitHub Desktop.
From 9ec8d502ea63809c01a86eb68e6a905a6321ad78 Mon Sep 17 00:00:00 2001
From: Gavin Stark <g.stark@realdigitalmedia.com>
Date: Sat, 12 Jun 2010 01:10:23 -0400
Subject: [PATCH] Dir#new should call #to_str under 1.8 and #to_path under 1.9
---
kernel/bootstrap/dir.rb | 4 ++--
kernel/common/dir.rb | 6 ++++++
spec/ruby/core/dir/dir_spec.rb | 29 +++++++++++++++++++++++++++++
3 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/kernel/bootstrap/dir.rb b/kernel/bootstrap/dir.rb
index 9dc7320..4231386 100644
--- a/kernel/bootstrap/dir.rb
+++ b/kernel/bootstrap/dir.rb
@@ -4,12 +4,12 @@ class Dir
raise PrimitiveFailure, "Dir.allocate primitive failed"
end
- def initialize(path)
+ def primitive_initialize(path)
Ruby.primitive :dir_open
raise PrimitiveFailure, "Dir#open primitive failed"
end
- private :initialize
+ private :primitive_initialize
def close
Ruby.primitive :dir_close
diff --git a/kernel/common/dir.rb b/kernel/common/dir.rb
index b11cbaf..f95900e 100644
--- a/kernel/common/dir.rb
+++ b/kernel/common/dir.rb
@@ -7,6 +7,12 @@ class Dir
files
end
+ def initialize(path)
+ path = StringValue path
+
+ primitive_initialize(path)
+ end
+
def self.glob(pattern, flags=0)
pattern = StringValue pattern
diff --git a/spec/ruby/core/dir/dir_spec.rb b/spec/ruby/core/dir/dir_spec.rb
index 4923445..5a77c79 100644
--- a/spec/ruby/core/dir/dir_spec.rb
+++ b/spec/ruby/core/dir/dir_spec.rb
@@ -1,7 +1,36 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../fixtures/common', __FILE__)
describe "Dir" do
it "includes Enumerable" do
Dir.include?(Enumerable).should == true
end
+
+ describe "#initialize" do
+ before :each do
+ DirSpecs.create_mock_dirs
+ end
+
+ after :each do
+ DirSpecs.delete_mock_dirs
+ end
+
+ ruby_version_is ""..."1.9" do
+ it "#initialize calls #to_str on non-String arguments" do
+ p = mock('path')
+ p.stub!(:to_str).and_return(DirSpecs.mock_dir)
+
+ Dir.new(p).path.should == DirSpecs.mock_dir
+ end
+ end
+
+ ruby_version_is "1.9" do
+ it "calls #to_path on non-String arguments" do
+ p = mock('path')
+ p.stub!(:to_path).and_return(DirSpecs.mock_dir)
+ Dir.new(p).path.should == DirSpecs.mock_dir
+ end
+ end
+ end
+
end
--
1.7.1
From 72ae75ab7223f7b53211cb151ef71ff9363f1775 Mon Sep 17 00:00:00 2001
From: Gavin Stark <g.stark@realdigitalmedia.com>
Date: Fri, 11 Jun 2010 17:49:38 -0400
Subject: [PATCH] Dir should be calling to_str on non-String arguments in Ruby 1.8
---
spec/ruby/core/dir/shared/open.rb | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)
diff --git a/spec/ruby/core/dir/shared/open.rb b/spec/ruby/core/dir/shared/open.rb
index d757451..b48eba9 100644
--- a/spec/ruby/core/dir/shared/open.rb
+++ b/spec/ruby/core/dir/shared/open.rb
@@ -37,6 +37,14 @@ describe :dir_open, :shared => true do
lambda { @closed_dir.close }.should raise_error(IOError)
end
+ ruby_version_is "1.8" do
+ it "calls #to_str on non-String arguments" do
+ p = mock('path')
+ p.should_receive(:to_str).and_return(DirSpecs.mock_dir)
+ Dir.send(@method, p) { true }
+ end
+ end
+
ruby_version_is "1.9" do
it "calls #to_path on non-String arguments" do
p = mock('path')
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment