ruby-nuggets 0.3.0.275 → 0.3.1.277

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.3.0
5
+ This documentation refers to ruby-nuggets version 0.3.1
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -0,0 +1,56 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2008 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ class Array
29
+
30
+ # call-seq:
31
+ # array.only([relax]) => anObject
32
+ #
33
+ # Returns the only element of _array_. Raises an IndexError if _array_'s
34
+ # size is not 1, unless +relax+ is true.
35
+ #
36
+ # Idea stolen from Gavin Sinclair's Ruby Extensions Project.
37
+ def only(relax = size == 1)
38
+ raise IndexError, 'not a single-element array' unless relax
39
+ first
40
+ end
41
+
42
+ end
43
+
44
+ if $0 == __FILE__
45
+ [[5], [1, 2, 3], []].each { |a|
46
+ p a
47
+
48
+ begin
49
+ p a.only
50
+ rescue IndexError => err
51
+ warn err
52
+ end
53
+
54
+ p a.only(true)
55
+ }
56
+ end
@@ -0,0 +1,87 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2008 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ require File.join(File.dirname(__FILE__), '..', 'array', 'rand')
29
+
30
+ class Hash
31
+
32
+ # call-seq:
33
+ # hash.at(what) => aHash
34
+ #
35
+ # Returns the key/value pair of _hash_ at key position +what+. Remember that
36
+ # hashes might not have the intended (or expected) order in pre-1.9 Ruby.
37
+ def at(what)
38
+ return {} if empty?
39
+
40
+ key = case what
41
+ when Integer
42
+ keys[what]
43
+ else
44
+ block_given? ? keys.send(*what) { |*a| yield(*a) } : keys.send(*what)
45
+ end
46
+
47
+ { key => self[key] }
48
+ end
49
+
50
+ # call-seq:
51
+ # hash.first => aHash
52
+ #
53
+ # Returns the "first" key/value pair of _hash_.
54
+ def first
55
+ at(:first)
56
+ end
57
+
58
+ # call-seq:
59
+ # hash.last => aHash
60
+ #
61
+ # Returns the "last" key/value pair of _hash_.
62
+ def last
63
+ at(:last)
64
+ end
65
+
66
+ # call-seq:
67
+ # hash.rand => aHash
68
+ #
69
+ # Returns a random key/value pair of _hash_.
70
+ def rand
71
+ at(:rand)
72
+ end
73
+
74
+ end
75
+
76
+ if $0 == __FILE__
77
+ h = { :a => 1, 2 => 3, nil => nil, 'foo' => %w[b a r]}
78
+ p h
79
+
80
+ p h.first
81
+ p h.last
82
+ p h.rand
83
+
84
+ p h.at(0)
85
+ p h.at(1)
86
+ p h.at(-1)
87
+ end
@@ -0,0 +1,68 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2008 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ require File.join(File.dirname(__FILE__), 'at')
29
+
30
+ class Hash
31
+
32
+ # call-seq:
33
+ # hash.only([relax]) => aHash
34
+ #
35
+ # Returns the only key/value pair of _hash_. Raises an IndexError if _hash_'s
36
+ # size is not 1, unless +relax+ is true.
37
+ def only(relax = size == 1, split = false)
38
+ raise IndexError, 'not a single-element hash' unless relax
39
+
40
+ return *first if split
41
+ first
42
+ end
43
+
44
+ # call-seq:
45
+ # hash.only_pair([relax]) => anArray
46
+ #
47
+ # Returns the only key/value pair of _hash_ as an array. Raises an IndexError
48
+ # if _hash_'s size is not 1, unless +relax+ is true.
49
+ def only_pair(relax = size == 1)
50
+ only(relax, true)
51
+ end
52
+
53
+ end
54
+
55
+ if $0 == __FILE__
56
+ [{ :a => 5 }, { 1 => 2, 3 => 4 }, {}].each { |h|
57
+ p h
58
+
59
+ begin
60
+ p h.only
61
+ p h.only_pair
62
+ rescue IndexError => err
63
+ warn err
64
+ end
65
+
66
+ p h.only(true)
67
+ }
68
+ end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 3
7
- TINY = 0
7
+ TINY = 1
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.275
4
+ version: 0.3.1.277
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-15 00:00:00 +02:00
12
+ date: 2008-08-19 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -47,12 +47,15 @@ files:
47
47
  - lib/nuggets/string/word_wrap.rb
48
48
  - lib/nuggets/string/nsub.rb
49
49
  - lib/nuggets/string/capitalize_first.rb
50
+ - lib/nuggets/hash/at.rb
51
+ - lib/nuggets/hash/only.rb
50
52
  - lib/nuggets/hash/in_order.rb
51
53
  - lib/nuggets/hash/insert.rb
52
54
  - lib/nuggets/proc/bind.rb
53
55
  - lib/nuggets/array/rand.rb
54
56
  - lib/nuggets/array/to_hash.rb
55
57
  - lib/nuggets/array/flatten_once.rb
58
+ - lib/nuggets/array/only.rb
56
59
  - lib/nuggets/array/in_order.rb
57
60
  - lib/nuggets/array/shuffle.rb
58
61
  - lib/nuggets/array/monotone.rb