ruby-nuggets 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --require spec/spec_helper.rb
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.6.5
5
+ This documentation refers to ruby-nuggets version 0.6.6
6
6
 
7
7
 
8
8
  == DESCRIPTION
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ begin
13
13
  :version => Nuggets::VERSION,
14
14
  :summary => 'Some extensions to the Ruby programming language.',
15
15
  :files => FileList['lib/**/*.rb'].to_a,
16
- :extra_files => FileList['[A-Z]*'].to_a
16
+ :extra_files => FileList['[A-Z]*', '.rspec', 'spec/**/*.rb'].to_a
17
17
  }
18
18
  }}
19
19
  rescue LoadError => err
@@ -25,24 +25,36 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
+ require 'rbconfig'
29
+
28
30
  module Nuggets
29
31
  class File
30
32
  module WhichMixin
31
33
 
34
+ DEFAULT_EXTENSIONS = [Config::CONFIG['EXEEXT']]
35
+
32
36
  # call-seq:
33
- # File.which(executable) => aString or nil
37
+ # File.which(executable, extensions = DEFAULT_EXTENSIONS) => aString or nil
38
+ #
39
+ # Returns +executable+ if it's executable, or the full path to +executable+
40
+ # found in PATH, or +nil+ otherwise. Checks +executable+ with each extension
41
+ # in +extensions+ appended in turn.
34
42
  #
35
- # Returns the full path to +executable+, or +nil+ if not found in PATH.
36
43
  # Inspired by Gnuplot.which -- thx, Gordon!
37
- def which(executable)
38
- return executable if executable?(executable)
39
-
40
- if path = ENV['PATH']
41
- path.split(::File::PATH_SEPARATOR).each { |dir|
42
- candidate = join(expand_path(dir), executable)
43
- return candidate if executable?(candidate)
44
- }
45
- end
44
+ def which(executable, extensions = DEFAULT_EXTENSIONS)
45
+ extensions |= ['']
46
+
47
+ extensions.each { |extension|
48
+ executable += extension
49
+ return executable if executable?(executable)
50
+
51
+ if path = ENV['PATH']
52
+ path.split(::File::PATH_SEPARATOR).each { |dir|
53
+ candidate = join(expand_path(dir), executable)
54
+ return candidate if executable?(candidate)
55
+ }
56
+ end
57
+ }
46
58
 
47
59
  nil
48
60
  end
@@ -50,9 +62,9 @@ module Nuggets
50
62
  # call-seq:
51
63
  # File.which_command(commands) => aString or nil
52
64
  #
53
- # Returns the first of +commands+ that is executable.
54
- def which_command(commands)
55
- commands.find { |command| which(command[/\S+/]) }
65
+ # Returns the first of +commands+ that is executable (according to #which).
66
+ def which_command(commands, extensions = DEFAULT_EXTENSIONS)
67
+ commands.find { |command| which(command[/\S+/], extensions) }
56
68
  end
57
69
 
58
70
  end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 6
7
- TINY = 5
7
+ TINY = 6
8
8
 
9
9
  class << self
10
10
 
@@ -0,0 +1,36 @@
1
+ require 'nuggets/array/limit'
2
+ require 'nuggets/numeric/limit'
3
+
4
+ describe Array, 'when extended by', Nuggets::Array::LimitMixin do
5
+
6
+ it { Array.ancestors.should include(Nuggets::Array::LimitMixin) }
7
+
8
+ example do
9
+ [].limit(0, 1).should == []
10
+ end
11
+
12
+ example do
13
+ [1].limit(0, 1).should == [1]
14
+ end
15
+
16
+ example do
17
+ [1, 1, 1].limit(0, 1).should == [1]
18
+ end
19
+
20
+ example do
21
+ [1, 2, 3].limit(0, 1).should == [1]
22
+ end
23
+
24
+ example do
25
+ [1, 2, 3].limit(0, 2).should == [1, 2]
26
+ end
27
+
28
+ example do
29
+ [-3, -2, -1].limit(0, 1).should == [0]
30
+ end
31
+
32
+ example do
33
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].limit(-2, 2).should == [1, -2, 2, 0]
34
+ end
35
+
36
+ end
@@ -0,0 +1,27 @@
1
+ require 'nuggets/array/runiq'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::RuniqMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::RuniqMixin) }
6
+
7
+ example do
8
+ [1, 2, 3, 4, 3, 2].runiq.should == [1, 4, 3, 2]
9
+ end
10
+
11
+ example do
12
+ ary = [1, 2, 3, 4, 3, 2]
13
+ ary.runiq!.should_not be_nil
14
+ ary.should == [1, 4, 3, 2]
15
+ end
16
+
17
+ example do
18
+ [1, 2, 3, 4].runiq.should == [1, 2, 3, 4]
19
+ end
20
+
21
+ example do
22
+ ary = [1, 2, 3, 4]
23
+ ary.runiq!.should be_nil
24
+ ary.should == [1, 2, 3, 4]
25
+ end
26
+
27
+ end
@@ -0,0 +1,35 @@
1
+ require 'nuggets/array/standard_deviation'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::StandardDeviationMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::StandardDeviationMixin) }
6
+
7
+ example do
8
+ [].standard_deviation.should == 0.0
9
+ end
10
+
11
+ example do
12
+ [1].standard_deviation.should == 0.0
13
+ end
14
+
15
+ example do
16
+ [1, 1, 1].standard_deviation.should == 0.0
17
+ end
18
+
19
+ example do
20
+ [1, 2, 3].standard_deviation.should be_within(0.0001).of(0.8165)
21
+ end
22
+
23
+ example do
24
+ [3, 2, 1].standard_deviation.should be_within(0.0001).of(0.8165)
25
+ end
26
+
27
+ example do
28
+ [-3, -2, -1].standard_deviation.should == [3, 2, 1].standard_deviation
29
+ end
30
+
31
+ example do
32
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].standard_deviation.should be_within(0.01).of(6.49)
33
+ end
34
+
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'nuggets/array/variance'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::VarianceMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::VarianceMixin) }
6
+
7
+ example do
8
+ [].variance.should == 0.0
9
+ end
10
+
11
+ example do
12
+ [1].variance.should == 0.0
13
+ end
14
+
15
+ example do
16
+ [1, 1, 1].variance.should == 0.0
17
+ end
18
+
19
+ example do
20
+ [1, 2, 3].variance.should == 2 / 3.0
21
+ end
22
+
23
+ example do
24
+ [3, 2, 1].variance.should == 2 / 3.0
25
+ end
26
+
27
+ example do
28
+ [-3, -2, -1].variance.should == [3, 2, 1].variance
29
+ end
30
+
31
+ example do
32
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].variance.should be_within(0.1).of(42.0)
33
+ end
34
+
35
+ end
@@ -0,0 +1,31 @@
1
+ require 'nuggets/env/set'
2
+
3
+ describe ENV, 'when extended by', Nuggets::Env::SetMixin do
4
+
5
+ it { class << ENV; ancestors; end.should include(Nuggets::Env::SetMixin) }
6
+
7
+ before do
8
+ @original = ENV.to_hash
9
+ end
10
+
11
+ after do
12
+ ENV.clear
13
+ ENV.update(@original)
14
+ end
15
+
16
+ example do
17
+ ENV.with(:lang => 'C') { ENV['LANG'].should == 'C' }
18
+ ENV['LANG'].should == @original['LANG']
19
+ end
20
+
21
+ example do
22
+ ENV.set(:lang => 'C').to_hash.should == @original
23
+ ENV.to_hash.should == { 'LANG' => 'C' }
24
+ end
25
+
26
+ example do
27
+ ENV.set(@original)
28
+ ENV.to_hash.should == @original
29
+ end
30
+
31
+ end
@@ -0,0 +1,40 @@
1
+ require 'nuggets/env/user_encoding'
2
+
3
+ describe ENV, 'when extended by', Nuggets::Env::UserEncodingMixin do
4
+
5
+ it { class << ENV; ancestors; end.should include(Nuggets::Env::UserEncodingMixin) }
6
+
7
+ before do
8
+ @old_env = ENV.to_hash
9
+ end
10
+
11
+ after do
12
+ ENV.clear
13
+ ENV.update(@old_env)
14
+ end
15
+
16
+ example do
17
+ ENV.user_encoding.should be_an_instance_of(String)
18
+ end
19
+
20
+ example do
21
+ ENV.clear
22
+ ENV.user_encoding.should be_an_instance_of(String)
23
+ end
24
+
25
+ example do
26
+ ENV['ENCODING'] = 'foo'
27
+ ENV.user_encoding.should == 'foo'
28
+ end
29
+
30
+ example do
31
+ ENV.clear
32
+ ENV.user_encoding('bar').should == 'bar'
33
+ end
34
+
35
+ example do
36
+ ENV.clear
37
+ ENV.user_encoding(nil).should be_nil
38
+ end
39
+
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'nuggets/env/user_home'
2
+
3
+ describe ENV, 'when extended by', Nuggets::Env::UserHomeMixin do
4
+
5
+ it { class << ENV; ancestors; end.should include(Nuggets::Env::UserHomeMixin) }
6
+
7
+ before do
8
+ @old_env = ENV.to_hash
9
+ end
10
+
11
+ after do
12
+ ENV.clear
13
+ ENV.update(@old_env)
14
+ end
15
+
16
+ example do
17
+ ENV.user_home.should be_an_instance_of(String)
18
+ end
19
+
20
+ example do
21
+ ENV.clear
22
+ ENV.user_home.should be_an_instance_of(String)
23
+ end
24
+
25
+ example do
26
+ ENV['HOME'] = 'foo'
27
+ ENV.user_home.should == 'foo'
28
+ end
29
+
30
+ example do
31
+ ENV.clear
32
+ ENV.user_home('bar').should == 'bar'
33
+ end
34
+
35
+ example do
36
+ ENV.clear
37
+ ENV.user_home(nil).should be_nil
38
+ end
39
+
40
+ end
@@ -0,0 +1,24 @@
1
+ require 'nuggets/file/which'
2
+
3
+ describe File, 'when extended by', Nuggets::File::WhichMixin do
4
+
5
+ it { (class << File; ancestors; end).should include(Nuggets::File::WhichMixin) }
6
+
7
+ %w[cat dog rat gcc /usr/bin/X11/gcc].each { |c|
8
+ example do
9
+ r = %x{which #{c}}.chomp
10
+ File.which(c).should == (r.empty? ? nil : r)
11
+ end
12
+ }
13
+
14
+ example do
15
+ c = [
16
+ 'unison --args source target',
17
+ 'rsync --args source target',
18
+ 'scp --args source target'
19
+ ]
20
+ r = c.find { |s| !%x{which #{s[/\S+/]}}.chomp.empty? }
21
+ File.which_command(c).should == r
22
+ end
23
+
24
+ end
@@ -0,0 +1,104 @@
1
+ require 'nuggets/hash/nest'
2
+
3
+ describe Hash, 'when extended by', Nuggets::Hash::NestMixin do
4
+
5
+ it { (class << Hash; self; end).ancestors.should include(Nuggets::Hash::NestMixin) }
6
+
7
+ example do
8
+ hash = Hash.nest
9
+ hash.should be_an_instance_of(Hash)
10
+
11
+ hash[:a].should be_nil
12
+ hash.should have_key(:a)
13
+ end
14
+
15
+ example do
16
+ hash = Hash.nest(0, 1)
17
+ hash.should be_an_instance_of(Hash)
18
+
19
+ hash[:a].should == 1
20
+ hash.should have_key(:a)
21
+ end
22
+
23
+ example do
24
+ hash = Hash.nest(1)
25
+ hash.should be_an_instance_of(Hash)
26
+
27
+ hash[:a].should be_an_instance_of(Hash)
28
+ hash.should have_key(:a)
29
+
30
+ hash[:a][:b].should be_nil
31
+ hash[:a].should have_key(:b)
32
+ end
33
+
34
+ example do
35
+ hash = Hash.nest(1, 1)
36
+ hash.should be_an_instance_of(Hash)
37
+
38
+ hash[:a].should be_an_instance_of(Hash)
39
+ hash.should have_key(:a)
40
+
41
+ hash[:a][:b].should == 1
42
+ hash[:a].should have_key(:b)
43
+ end
44
+
45
+ example do
46
+ hash = Hash.nest(1, [])
47
+ hash.should be_an_instance_of(Hash)
48
+
49
+ hash[:a].should be_an_instance_of(Hash)
50
+ hash.should have_key(:a)
51
+
52
+ hash[:a][:b].should == []
53
+ hash[:a].should have_key(:b)
54
+
55
+ hash[:a][:b] << 1
56
+ hash[:a][:b].should == [1]
57
+
58
+ hash[:a][:c] << 2
59
+ hash[:a][:c].should == [1, 2]
60
+
61
+ hash[:a][:b].should == [1, 2]
62
+ end
63
+
64
+ example do
65
+ hash = Hash.nest(1) { [] }
66
+ hash.should be_an_instance_of(Hash)
67
+
68
+ hash[:a].should be_an_instance_of(Hash)
69
+ hash.should have_key(:a)
70
+
71
+ hash[:a][:b].should == []
72
+ hash[:a].should have_key(:b)
73
+
74
+ hash[:a][:b] << 1
75
+ hash[:a][:b].should == [1]
76
+
77
+ hash[:a][:c] << 2
78
+ hash[:a][:c].should == [2]
79
+
80
+ hash[:a][:b].should == [1]
81
+ end
82
+
83
+ example do
84
+ hash = Hash.nest(3)
85
+ hash.should be_an_instance_of(Hash)
86
+
87
+ hash[:a].should be_an_instance_of(Hash)
88
+ hash.should have_key(:a)
89
+
90
+ hash[:a][:b].should be_an_instance_of(Hash)
91
+ hash[:a].should have_key(:b)
92
+
93
+ hash[:a][:b][:c].should be_an_instance_of(Hash)
94
+ hash[:a][:b].should have_key(:c)
95
+
96
+ hash[:a][:b][:c][:d].should be_nil
97
+ hash[:a][:b][:c].should have_key(:d)
98
+
99
+ hash[:a][:b][:c][:e] = 1
100
+ hash[:a][:b][:c][:e].should == 1
101
+ hash[:a][:b][:c].should have_key(:e)
102
+ end
103
+
104
+ end
@@ -0,0 +1,70 @@
1
+ require 'nuggets/hash/unroll'
2
+
3
+ describe Hash, 'when extended by', Nuggets::Hash::UnrollMixin do
4
+
5
+ it { Hash.ancestors.should include(Nuggets::Hash::UnrollMixin) }
6
+
7
+ example do
8
+ hash = {}
9
+ hash.unroll.should == [[]]
10
+ end
11
+
12
+ example do
13
+ hash = { 'a' => 1 }
14
+ hash.unroll.should == [[1]]
15
+ end
16
+
17
+ example do
18
+ hash = { 'a' => { 'b' => 1 } }
19
+ hash.unroll.should == [['a', 1]]
20
+ end
21
+
22
+ example do
23
+ hash = { :a => { :b => 1 } }
24
+ hash.unroll.should == [[:a, 1]]
25
+ end
26
+
27
+ example do
28
+ hash = { :a => { :b => 1, :c => 2 } }
29
+
30
+ result = hash.unroll.first
31
+ result.should have(3).items
32
+
33
+ result.first.should == :a
34
+ result.should include(1)
35
+ result.should include(2)
36
+ end
37
+
38
+ if RUBY_VERSION < '1.9'
39
+ example do
40
+ hash = { :a => { :b => 1, :c => 2 } }
41
+ lambda { hash.unroll(:sort => true) }.should raise_error(NoMethodError, /<=>/)
42
+ end
43
+
44
+ example do
45
+ hash = { :a => { :b => 1, :c => 2 } }
46
+ lambda { hash.unroll(:sort_by => lambda { |h| h.to_s }) }.should_not raise_error(NoMethodError)
47
+ end
48
+ end
49
+
50
+ example do
51
+ hash = { 'a' => { 'b' => 1, 'c' => 2 } }
52
+ hash.unroll(:sort => true).should == [['a', 1, 2]]
53
+ end
54
+
55
+ example do
56
+ hash = { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } }
57
+ hash.unroll('b', 'c', :sort => true).should == [['a', 1, 2], ['d', 0, 3]]
58
+ end
59
+
60
+ example do
61
+ hash = { 'z' => { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } } }
62
+ hash.unroll('b', :sort_by => lambda { |h| h.to_s }).should == [['z', 'a', 1], ['z', 'd', 0]]
63
+ end
64
+
65
+ example do
66
+ hash = { 'z' => { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } } }
67
+ hash.unroll(:sort => true) { |h| h['b'] = nil; h['c'] *= 2 }.should == [['z', 'a', nil, 4], ['z', 'd', nil, 6]]
68
+ end
69
+
70
+ end
@@ -0,0 +1,20 @@
1
+ require 'nuggets/integer/length'
2
+
3
+ describe Integer, 'when extended by', Nuggets::Integer::LengthMixin do
4
+
5
+ it { Integer.ancestors.should include(Nuggets::Integer::LengthMixin) }
6
+
7
+ {
8
+ 0 => [1, 1],
9
+ 123 => [3, 3],
10
+ -123 => [4, 3],
11
+ 1_000_000 => [7, 7],
12
+ -1_000_000 => [8, 7],
13
+ 10 ** 48 => [49, 49],
14
+ -(10 ** 48) => [50, 49]
15
+ }.each { |int, (len, cnt)|
16
+ example { int.length.should == len }
17
+ example { int.digit_count.should == cnt }
18
+ }
19
+
20
+ end
@@ -0,0 +1,21 @@
1
+ require 'nuggets/integer/map'
2
+
3
+ describe Integer, 'when extended by', Nuggets::Integer::MapMixin do
4
+
5
+ it { Integer.ancestors.should include(Nuggets::Integer::MapMixin) }
6
+
7
+ {
8
+ 0 => 1,
9
+ 1 => 2,
10
+ -1 => 3,
11
+ 123 => 246,
12
+ -123 => 247,
13
+ 1_000_000 => 2_000_000,
14
+ -1_000_000 => 2_000_001,
15
+ 10 ** 48 => 2_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000,
16
+ -(10 ** 48) => 2_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_001
17
+ }.each { |int, map|
18
+ example { int.map_positive.should == map }
19
+ }
20
+
21
+ end
@@ -0,0 +1,35 @@
1
+ require 'nuggets/object/blank'
2
+
3
+ describe Object, 'when extended by', Nuggets::Object::BlankMixin do
4
+
5
+ it { Object.ancestors.should include(Nuggets::Object::BlankMixin) }
6
+ it { Array.ancestors.should include(Nuggets::Array::BlankMixin) }
7
+ it { Hash.ancestors.should include(Nuggets::Hash::BlankMixin) }
8
+
9
+ ['s', ' ', 0, 1, true, [nil]].each { |o|
10
+ example { o.should_not be_blank }
11
+ }
12
+
13
+ ['', nil, false, [], {}].each { |o|
14
+ example { o.should be_blank }
15
+ }
16
+
17
+ ['s', 1, true].each { |o|
18
+ example { o.should_not be_void }
19
+ example { o.should_not be_vain }
20
+ }
21
+
22
+ ['', ' ', 0, nil, false, [], [nil], {}].each { |o|
23
+ example { o.should be_void }
24
+ example { o.should be_vain }
25
+ }
26
+
27
+ [['', [], [nil], {}], { :x => nil, :y => [], :z => { :zz => nil } }].each { |o|
28
+ example { o.should_not be_void }
29
+ }
30
+
31
+ [['', [], [nil], {}], { :x => nil, :y => [], :z => { :zz => nil } }].each { |o|
32
+ example { o.should be_vain }
33
+ }
34
+
35
+ end
@@ -0,0 +1,25 @@
1
+ require 'nuggets/object/boolean'
2
+
3
+ describe Object, 'when extended by', Nuggets::Object::BooleanMixin do
4
+
5
+ it { Object.ancestors.should include(Nuggets::Object::BooleanMixin) }
6
+
7
+ [0, 1, -1, nil, '', 'abc', Class, Object.new].each { |o|
8
+ example { o.should_not be_boolean }
9
+ }
10
+
11
+ [true, false].each { |o|
12
+ example { o.should be_boolean }
13
+ }
14
+
15
+ [0, 1, -1, '', 'abc', Class, Object.new, true].each { |o|
16
+ example { o.negate.should == false }
17
+ example { o.to_bool.should == true }
18
+ }
19
+
20
+ [nil, false].each { |o|
21
+ example { o.negate.should == true }
22
+ example { o.to_bool.should == false }
23
+ }
24
+
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'nuggets/object/msend'
2
+
3
+ describe Object, 'when extended by', Nuggets::Object::MSendMixin do
4
+
5
+ it { Object.ancestors.should include(Nuggets::Object::MSendMixin) }
6
+
7
+ example do
8
+ o = 'foo bar'
9
+ o.msend(:length, :reverse).should == [o.length, o.reverse]
10
+ end
11
+
12
+ example do
13
+ o = 42
14
+ o.msend(:to_s, :* => 2).should == [o.to_s, o * 2]
15
+ end
16
+
17
+ example do
18
+ o = 42
19
+ o.msend([:to_s, 2], '-@').should == [o.to_s(2), -o]
20
+ end
21
+
22
+ example do
23
+ o = Time.now
24
+ o.msend(:year, :month, :day).should == [o.year, o.month, o.day]
25
+ end
26
+
27
+ end
@@ -0,0 +1,33 @@
1
+ require 'nuggets/object/silence'
2
+
3
+ describe Object, 'when extended by', Nuggets::Object::SilenceMixin do
4
+
5
+ it { Object.ancestors.should include(Nuggets::Object::SilenceMixin) }
6
+
7
+ before do
8
+ @stderr, @old_stderr = '', $stderr
9
+ $stderr = StringIO.new(@stderr)
10
+
11
+ AConst = :foo
12
+ end
13
+
14
+ after do
15
+ $stderr = @old_stderr
16
+ Object.send(:remove_const, :AConst) if Object.const_defined?(:AConst)
17
+ end
18
+
19
+ example do
20
+ AConst = :bar
21
+
22
+ AConst.should be_equal(:bar)
23
+ @stderr.should =~ /warning: already initialized constant AConst/
24
+ end
25
+
26
+ example do
27
+ silence { AConst = :baz }
28
+
29
+ AConst.should be_equal(:baz)
30
+ @stderr.should == ''
31
+ end
32
+
33
+ end
@@ -0,0 +1,44 @@
1
+ require 'nuggets/object/singleton_class'
2
+
3
+ describe Object, 'when extended by', Nuggets::Object::SingletonClassMixin do
4
+
5
+ it { Object.ancestors.should include(Nuggets::Object::SingletonClassMixin) }
6
+
7
+ [Object, Class, Object.new, Class.new, 's', [1, 2], { :a => 'b' }].each { |o|
8
+ example { o.should_not be_a_singleton_class }
9
+ example { lambda { o.singleton_object }.should raise_error(TypeError) }
10
+
11
+ s = o.singleton_class
12
+
13
+ example { s.should be_a_singleton_class }
14
+ example { s.singleton_object.should be_equal(o) }
15
+ }
16
+
17
+ example do
18
+ c = Class.new
19
+ c.should_not be_a_singleton_class
20
+ o = c.new
21
+ c.should_not be_a_singleton_class
22
+ end
23
+
24
+ example do
25
+ nil.singleton_class.should == NilClass
26
+ NilClass.should be_a_singleton_class
27
+ NilClass.singleton_object.should be_equal(nil)
28
+ end
29
+
30
+ example do
31
+ class A; end
32
+ class B < A; end
33
+
34
+ a = A.singleton_class
35
+ b = B.singleton_class
36
+
37
+ a.should be_a_singleton_class
38
+ b.should be_a_singleton_class
39
+
40
+ a.singleton_object.should be_equal(A)
41
+ b.singleton_object.should be_equal(B)
42
+ end
43
+
44
+ end
@@ -0,0 +1,30 @@
1
+ require 'nuggets/proc/bind'
2
+
3
+ describe Proc, 'when extended by', Nuggets::Proc::BindMixin do
4
+
5
+ it { Proc.ancestors.should include(Nuggets::Proc::BindMixin) }
6
+
7
+ before :each do
8
+ @l = lambda { bla }
9
+ end
10
+
11
+ example do
12
+ lambda { @l.call }.should raise_error(NameError)
13
+ end
14
+
15
+ example do
16
+ module Foo; def self.bla; 'bar'; end; end
17
+ @l.bind(Foo).call.should == 'bar'
18
+ end
19
+
20
+ example do
21
+ class Bar; def self.bla; 'baz'; end; end
22
+ @l.bind(Bar).call.should == 'baz'
23
+ end
24
+
25
+ example do
26
+ class Baz; def bla; 'foo'; end; end
27
+ @l.bind(Baz.new).call.should == 'foo'
28
+ end
29
+
30
+ end
@@ -0,0 +1,35 @@
1
+ require 'nuggets/range/quantile'
2
+
3
+ describe Range, 'when extended by', Nuggets::Range::QuantileMixin do
4
+
5
+ it { Range.ancestors.should include(Nuggets::Range::QuantileMixin) }
6
+
7
+ example do
8
+ (1..5).quantile(0, 3).should == 1
9
+ end
10
+
11
+ example do
12
+ (1..5).quantile(1, 3).should == 1
13
+ end
14
+
15
+ example do
16
+ (1..5).quantile(2, 3).should == 1
17
+ end
18
+
19
+ example do
20
+ (1..5).quantile(3, 3).should == 2
21
+ end
22
+
23
+ example do
24
+ (1..5).quantile(4, 3).should == 3
25
+ end
26
+
27
+ example do
28
+ (1..5).quantile(5, 3).should == 3
29
+ end
30
+
31
+ example do
32
+ (1..5).quantile(6, 3).should == 3
33
+ end
34
+
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'nuggets/string/evaluate'
2
+
3
+ describe String, 'when extended by', Nuggets::String::EvaluateMixin do
4
+
5
+ it { String.ancestors.should include(Nuggets::String::EvaluateMixin) }
6
+
7
+ describe do
8
+
9
+ %w[bl#{a}blub bl#{a}#{b}lub].each { |str|
10
+ { 'blablub' => %w[a b], 'blubblub' => %w[ub b] }.each { |res, (a, b)|
11
+ example { str.evaluate(binding).should == res }
12
+ }
13
+
14
+ example {
15
+ lambda { str.evaluate(binding) }.should raise_error(NameError, /`a'/)
16
+ }
17
+ }
18
+
19
+ end
20
+
21
+ [ 'a"b"c', 'a\"b\"c', 'a\\"b\\"c', 'a\\\"b\\\"c',
22
+ "a'b'c", 'a%q{b}c', 'a{b}c', 'a{bc', 'ab}c' ].each { |str|
23
+ example { str.evaluate(binding).should == str }
24
+ }
25
+
26
+ end
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ require 'nuggets/string/wc'
4
+
5
+ describe String, 'when extended by', Nuggets::String::WcMixin do
6
+
7
+ it { String.ancestors.should include(Nuggets::String::WcMixin) }
8
+
9
+ describe 'empty' do
10
+
11
+ before do
12
+ @str = ''
13
+ end
14
+
15
+ example { @str.wc.should == [0, 0, 0] }
16
+
17
+ example { @str.line_count.should == 0 }
18
+
19
+ example { @str.word_count.should == 0 }
20
+
21
+ example { @str.byte_count.should == 0 }
22
+
23
+ example { @str.char_count.should == 0 }
24
+
25
+ end
26
+
27
+ describe 'non-empty' do
28
+
29
+ before do
30
+ @str = <<-STR
31
+ The fox went out on a chilly night / Prayed for the moon to give him light / For he had many a mile to go that night / Before he reached the town o
32
+ He ran til he came to a great big bin / Where the ducks and the geese were kept therein / Said, a couple of you are going to grease my chin / Before I leave this town o
33
+ He grabbed the grey goose by the neck / Throwed a duck across his back / He didn't mind the quack, quack, quack / And the legs all dangling down o
34
+ Then old mother Flipper-flopper jumped out of bed / Out of the window she cocked her head / Crying, John, John the grey goose is gone / and the fox is on the town o
35
+ Then John he went to the top of the hill / Blew his horn both loud and shrill / The fox, he said, I better flee with my kill / Or they'll soon be on my trail o
36
+ He ran till he came to his cozy den / There were the little ones, eight, nine, ten / Saying, Daddy, daddy, Better go back again / For it must be a mighty fine town o
37
+ Then the fox and his wife, without any strife / Cut up the goose with a carving knife / They never had such a supper in their life / And the little ones chewed on the bones o
38
+ STR
39
+ end
40
+
41
+ example { @str.wc.should == [7, 252, 1130] }
42
+
43
+ example { @str.line_count.should == 7 }
44
+
45
+ example { @str.word_count.should == 252 }
46
+
47
+ example { @str.byte_count.should == 1130 }
48
+
49
+ example { @str.char_count.should == 1130 }
50
+
51
+ end
52
+
53
+ describe 'special' do
54
+
55
+ before do
56
+ @str = <<-STR
57
+ TECHNICIÄNS ÖF SPÅCE SHIP EÅRTH
58
+ THIS IS YÖÜR CÄPTÅIN SPEÄKING
59
+ YÖÜR ØÅPTÅIN IS DEA̋D
60
+ STR
61
+ end
62
+
63
+ example { @str.wc.should == [3, 14, 99] }
64
+
65
+ example { @str.line_count.should == 3 }
66
+
67
+ example { @str.word_count.should == 14 }
68
+
69
+ example { @str.byte_count.should == 99 }
70
+
71
+ example { @str.char_count.should == 84 }
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,59 @@
1
+ require 'nuggets/string/xor'
2
+
3
+ describe String, 'when extended by', Nuggets::String::XorMixin do
4
+
5
+ it { String.ancestors.should include(Nuggets::String::XorMixin) }
6
+
7
+ describe 'same length' do
8
+
9
+ before do
10
+ @str1 = 'foobar'
11
+ @str2 = 'secret'
12
+
13
+ @res = "\025\n\f\020\004\006"
14
+ end
15
+
16
+ example { @str1.xor(@str2).should == @res }
17
+
18
+ example { @str2.xor(@str1).should == @res }
19
+
20
+ example { (@str1 ^ @str2).should == @res }
21
+
22
+ example { (@str2 ^ @str1).should == @res }
23
+
24
+ end
25
+
26
+ describe 'different length' do
27
+
28
+ before do
29
+ @str1 = 'baz'
30
+ @str2 = 'secret'
31
+ @str3 = 'foobarbaz'
32
+
33
+ @res1 = "sec\020\004\016"
34
+ @res2 = "foo\021\004\021\020\004\016"
35
+ end
36
+
37
+ example { @str1.xor(@str2).should == @res1 }
38
+
39
+ example { @str2.xor(@str1).should == @res1 }
40
+
41
+ example { (@str1 ^ @str2).should == @res1 }
42
+
43
+ example { (@str2 ^ @str1).should == @res1 }
44
+
45
+ example { lambda { @str1.xor(@str2, true) }.should raise_error(ArgumentError, 'must be of same length') }
46
+
47
+ example { @str2.xor(@str3).should == @res2 }
48
+
49
+ example { @str3.xor(@str2).should == @res2 }
50
+
51
+ example { (@str2 ^ @str3).should == @res2 }
52
+
53
+ example { (@str3 ^ @str2).should == @res2 }
54
+
55
+ example { lambda { @str2.xor(@str3, true) }.should raise_error(ArgumentError, 'must be of same length') }
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,32 @@
1
+ require 'nuggets/uri/content_type'
2
+
3
+ describe URI, 'when extended by', Nuggets::URI::ContentTypeMixin do
4
+
5
+ it { (class << URI; ancestors; end).should include(Nuggets::URI::ContentTypeMixin) }
6
+
7
+ %w[
8
+ http://www.google.de
9
+ http://blackwinter.de/misc/
10
+ ].each { |u|
11
+ example { URI.content_type(u).should == 'text/html' }
12
+ }
13
+
14
+ %w[
15
+ htp://www.google.de
16
+ www.google.de
17
+ http://blackwinter.de/bla
18
+ http://blawinter.de
19
+ ].each { |u|
20
+ example { URI.content_type(u).should == nil }
21
+ }
22
+
23
+ {
24
+ 'http://blackwinter.de/misc/ww.png' => 'image/png',
25
+ 'http://blackwinter.de/misc/suicide_is_painless.mid' => 'audio/midi',
26
+ 'http://blackwinter.de/misc/expand_macros.pl.gz' => 'application/x-gzip',
27
+ 'http://blackwinter.de/misc/blanc60302523.nth' => 'application/vnd.nok-s40theme'
28
+ }.each { |u, t|
29
+ example { URI.content_type(u).should == t }
30
+ }
31
+
32
+ end
@@ -0,0 +1,25 @@
1
+ require 'nuggets/uri/exist'
2
+
3
+ describe URI, 'when extended by', Nuggets::URI::ExistMixin do
4
+
5
+ it { (class << URI; ancestors; end).should include(Nuggets::URI::ExistMixin) }
6
+
7
+ %w[
8
+ http://www.google.de
9
+ http://blackwinter.de
10
+ http://blackwinter.de/index.html
11
+ ].each { |u|
12
+ example { URI.exist?(u).should be_true }
13
+ }
14
+
15
+ %w[
16
+ htp://www.google.de
17
+ www.google.de
18
+ http://xuugle.de
19
+ http://blackwinter.de/bla
20
+ http://blackwinter.de/index.htm
21
+ ].each { |u|
22
+ example { URI.exist?(u).should be_false }
23
+ }
24
+
25
+ end
@@ -0,0 +1,14 @@
1
+ $:.unshift('lib') unless $:[0] == 'lib'
2
+
3
+ #class << Spec::Matchers
4
+ #
5
+ # def generated_description
6
+ # return nil if last_should.nil?
7
+ #
8
+ # operator = last_should.to_s.tr('_', ' ')
9
+ # target = last_matcher.instance_variable_get(:@actual)
10
+ #
11
+ # "#{target.inspect} #{operator} #{last_description}"
12
+ # end
13
+ #
14
+ #end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 5
10
- version: 0.6.5
9
+ - 6
10
+ version: 0.6.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jens Wille
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-06 00:00:00 +01:00
18
+ date: 2011-01-07 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -129,21 +129,47 @@ files:
129
129
  - ChangeLog
130
130
  - Rakefile
131
131
  - COPYING
132
+ - .rspec
133
+ - spec/nuggets/integer/map_spec.rb
134
+ - spec/nuggets/integer/length_spec.rb
135
+ - spec/nuggets/array/variance_spec.rb
136
+ - spec/nuggets/array/runiq_spec.rb
137
+ - spec/nuggets/array/limit_spec.rb
138
+ - spec/nuggets/array/standard_deviation_spec.rb
139
+ - spec/nuggets/proc/bind_spec.rb
140
+ - spec/nuggets/hash/unroll_spec.rb
141
+ - spec/nuggets/hash/nest_spec.rb
142
+ - spec/nuggets/file/which_spec.rb
143
+ - spec/nuggets/string/evaluate_spec.rb
144
+ - spec/nuggets/string/xor_spec.rb
145
+ - spec/nuggets/string/wc_spec.rb
146
+ - spec/nuggets/env/user_encoding_spec.rb
147
+ - spec/nuggets/env/user_home_spec.rb
148
+ - spec/nuggets/env/set_spec.rb
149
+ - spec/nuggets/uri/content_type_spec.rb
150
+ - spec/nuggets/uri/exist_spec.rb
151
+ - spec/nuggets/range/quantile_spec.rb
152
+ - spec/nuggets/object/singleton_class_spec.rb
153
+ - spec/nuggets/object/boolean_spec.rb
154
+ - spec/nuggets/object/msend_spec.rb
155
+ - spec/nuggets/object/silence_spec.rb
156
+ - spec/nuggets/object/blank_spec.rb
157
+ - spec/spec_helper.rb
132
158
  has_rdoc: true
133
159
  homepage: http://prometheus.rubyforge.org/ruby-nuggets
134
160
  licenses: []
135
161
 
136
162
  post_install_message:
137
163
  rdoc_options:
138
- - --inline-source
139
- - --all
140
- - --charset
141
- - UTF-8
142
- - --main
143
- - README
144
164
  - --title
145
165
  - ruby-nuggets Application documentation
146
166
  - --line-numbers
167
+ - --main
168
+ - README
169
+ - --inline-source
170
+ - --charset
171
+ - UTF-8
172
+ - --all
147
173
  require_paths:
148
174
  - lib
149
175
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -167,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
193
  requirements: []
168
194
 
169
195
  rubyforge_project: prometheus
170
- rubygems_version: 1.4.1
196
+ rubygems_version: 1.4.2
171
197
  signing_key:
172
198
  specification_version: 3
173
199
  summary: Some extensions to the Ruby programming language.