Skip to content

Commit

Permalink
Merge branch 'upstream/sam'
Browse files Browse the repository at this point in the history
  • Loading branch information
mtodd committed Sep 2, 2008
2 parents 428227f + 6e0360b commit f94302d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/extlib.rb
Expand Up @@ -22,9 +22,12 @@

require dir / 'assertions'
require dir / 'blank'
require dir / 'boolean'
require dir / 'inflection'
require dir / 'lazy_array'
require dir / 'module'
require dir / 'nil'
require dir / 'numeric'
require dir / 'blank'
require dir / 'pooling'
require dir / 'simple_set'
Expand Down
11 changes: 11 additions & 0 deletions lib/extlib/boolean.rb
@@ -0,0 +1,11 @@
class TrueClass
def try_dup
self
end
end

class FalseClass
def try_dup
self
end
end
4 changes: 4 additions & 0 deletions lib/extlib/module.rb
Expand Up @@ -6,6 +6,10 @@ def find_const(const_name)
nested_const_lookup(const_name)
end
end

def try_dup
self
end

private

Expand Down
5 changes: 5 additions & 0 deletions lib/extlib/nil.rb
@@ -0,0 +1,5 @@
class NilClass
def try_dup
self
end
end
5 changes: 5 additions & 0 deletions lib/extlib/numeric.rb
@@ -0,0 +1,5 @@
class Numeric
def try_dup
self
end
end
7 changes: 7 additions & 0 deletions lib/extlib/object.rb
Expand Up @@ -129,6 +129,13 @@ def quacks_like?(duck)
end
end

# Override this in a child if it cannot be dup'ed
#
# @return <Object>
def try_dup
self.dup
end

# @param arrayish<#include?> Container to check, to see if it includes the object.
# @param *more<Array>:: additional args, will be flattened into arrayish
#
Expand Down
39 changes: 39 additions & 0 deletions spec/try_dup_spec.rb
@@ -0,0 +1,39 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))

describe "try_dup" do
it "returns a duplicate version on regular objects" do
obj = Object.new
oth = obj.try_dup
obj.should_not === oth
end

it "returns self on Numerics" do
obj = 12
oth = obj.try_dup
obj.should === oth
end

it "returns self on true" do
obj = true
oth = obj.try_dup
obj.should === oth
end

it "returns self on false" do
obj = false
oth = obj.try_dup
obj.should === oth
end

it "returns self on nil" do
obj = nil
oth = obj.try_dup
obj.should === oth
end

it "returns self on modules" do
obj = Extlib
oth = obj.try_dup
obj.object_id.should == oth.object_id
end
end

0 comments on commit f94302d

Please sign in to comment.