backports 1.6.8 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/backports/io.rb CHANGED
@@ -6,7 +6,49 @@ if RUBY_VERSION < '1.8.7'
6
6
  end
7
7
  end
8
8
 
9
- class << ARGF
10
- make_block_optional :each, :each_line, :each_byte
9
+ end
10
+
11
+ class IO
12
+ def bytes
13
+ to_enum :each_byte
14
+ end unless method_defined? :bytes
15
+
16
+ def chars
17
+ to_enum :each_char
18
+ end unless method_defined? :chars
19
+
20
+ def each_char
21
+ return to_enum(:each_char) unless block_given?
22
+ if $KCODE == "UTF-8"
23
+ lookup = 7.downto(4)
24
+ while c = read(1) do
25
+ n = c[0]
26
+ leftmost_zero_bit = lookup.find{|i| n[i].zero? }
27
+ case leftmost_zero_bit
28
+ when 7 # ASCII
29
+ yield c
30
+ when 6 # UTF 8 complementary characters
31
+ next # Encoding error, ignore
32
+ else
33
+ more = read(6-leftmost_zero_bit)
34
+ break unless more
35
+ yield c+more
36
+ end
37
+ end
38
+ else
39
+ while s = read(1)
40
+ yield s
41
+ end
42
+ end
43
+
44
+ self
11
45
  end
46
+
47
+ alias_method :getbyte, :getc unless method_defined? :getbyte
48
+ alias_method :readbyte, :readchar unless method_defined? :readchar
49
+
50
+ def lines(*args)
51
+ to_enum :each_line, *args
52
+ end unless method_defined? :lines
12
53
  end
54
+
@@ -1,6 +1,30 @@
1
+ require 'enumerator'
2
+ # Must be defined outside of Kernel for jruby, see http://jira.codehaus.org/browse/JRUBY-3609
3
+ Enumerator = Enumerable::Enumerator unless Kernel.const_defined? :Enumerator # Standard in ruby 1.9
4
+
1
5
  module Kernel
6
+
7
+ def __callee__
8
+ caller(1).first[/`(.*)'/,1].try(:to_sym)
9
+ end unless (__callee__ || true rescue false)
10
+
11
+ alias_method :__method__, :__callee__ unless (__method__ || true rescue false)
12
+
13
+ # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
14
+ def define_singleton_method(*args, &block)
15
+ class << self
16
+ self
17
+ end.send(:define_method, *args, &block)
18
+ end unless method_defined? :define_singleton_method
19
+
20
+ # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
21
+ def instance_exec(*arg, &block)
22
+ define_singleton_method(:"temporary method for instance_exec", &block)
23
+ send(:"temporary method for instance_exec", *arg)
24
+ end unless method_defined? :instance_exec
25
+
26
+ # Loop. Standard in ruby 1.8.7. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
2
27
  unless const_defined? :StopIteration
3
-
4
28
  class StopIteration < IndexError; end
5
29
 
6
30
  def loop_with_stop_iteration(&block)
@@ -9,12 +33,23 @@ module Kernel
9
33
  # ignore silently
10
34
  end
11
35
  alias_method_chain :loop, :stop_iteration
12
-
13
36
  end
14
-
15
- def __callee__
16
- caller(1).first[/`(.*)'/,1].try(:to_sym)
17
- end unless (__callee__ || true rescue false)
18
-
19
- alias_method :__method__, :__callee__ unless (__method__ || true rescue false)
37
+
38
+ # Standard in ruby 1.8.7. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
39
+ def tap
40
+ yield self
41
+ self
42
+ end unless method_defined? :tap
43
+
44
+ # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
45
+ def try(method_id, *args, &block)
46
+ send(method_id, *args, &block) unless self.nil? #todo: check new def
47
+ end unless method_defined? :try
48
+
49
+ # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
50
+ def returning(obj)
51
+ yield obj
52
+ obj
53
+ end unless method_defined? :returning
54
+
20
55
  end
@@ -27,24 +27,4 @@ class Module
27
27
  end unless method_defined? :module_exec
28
28
  alias_method :class_exec, :module_exec unless method_defined? :class_exec
29
29
 
30
- # Metaprogramming utility to make block optional.
31
- # Tests first if block is already optional when given options
32
- def make_block_optional(*methods)
33
- options = methods.extract_options!
34
- methods.each do |selector|
35
- next unless method_defined? selector
36
- unless options.empty?
37
- test_on = options[:test_on] || self.new
38
- next if (test_on.send(selector, *options.fetch(:arg, [])) rescue false)
39
- end
40
- alias_method_chain(selector, :optional_block) do |aliased_target, punctuation|
41
- module_eval <<-end_eval
42
- def #{aliased_target}_with_optional_block#{punctuation}(*args, &block)
43
- return to_enum(:#{aliased_target}_without_optional_block#{punctuation}, *args) unless block_given?
44
- #{aliased_target}_without_optional_block#{punctuation}(*args, &block)
45
- end
46
- end_eval
47
- end
48
- end
49
- end
50
30
  end
@@ -0,0 +1,3 @@
1
+ class Numeric
2
+ make_block_optional :step, :test_on => 42, :arg => [100, 6]
3
+ end
@@ -1,4 +1,3 @@
1
-
2
1
  module ObjectSpace
3
2
  module_eval do
4
3
  make_block_optional :each_object, :test_on => ObjectSpace
@@ -1,4 +1,4 @@
1
1
  class Proc
2
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Proc.html]
2
+ # Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Proc.html]
3
3
  alias_method :yield, :call unless method_defined? :yield
4
4
  end
@@ -0,0 +1,5 @@
1
+ module Process
2
+ def self.exec(*args)
3
+ raise NotImplementedError
4
+ end
5
+ end
@@ -1,16 +1,18 @@
1
1
  class String
2
2
  class << self
3
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
3
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
4
4
  def try_convert(x)
5
5
  return nil unless x.respond_do(:to_str)
6
6
  x.to_str
7
7
  end unless method_defined? :try_convert
8
8
  end
9
9
 
10
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
10
11
  def ascii_only?
11
12
  !(self =~ /[^\x00-\x7f]/)
12
13
  end unless method_defined? :ascii_only?
13
14
 
15
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
14
16
  alias_method :bytesize, :length unless method_defined? :bytesize
15
17
 
16
18
  # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
@@ -22,19 +24,18 @@ class String
22
24
  end
23
25
  end unless method_defined? :camelize
24
26
 
25
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
27
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
26
28
  def chr
27
29
  chars.first
28
- end unless method_defined? :chr?
30
+ end unless method_defined? :chr
29
31
 
30
-
31
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
32
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
32
33
  def clear
33
34
  self[0,length] = ""
34
35
  self
35
36
  end unless method_defined? :clear?
36
37
 
37
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
38
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
38
39
  def codepoints
39
40
  return to_enum(:codepoints) unless block_given?
40
41
  each_char.each do |s|
@@ -68,15 +69,11 @@ class String
68
69
 
69
70
  make_block_optional :each_byte, :each, :each_line, :test_on => "abc"
70
71
 
71
- # unless ("abc".gsub(/./) rescue false)
72
- # def gsub_with_optional_block(*arg, &block)
73
- # return to_enum(:gsub_with_optional_block, *arg, &block) unless block_given? || arg.size > 1
74
- # gsub_without_optional_block(*arg, &block)
75
- # end
76
- # alias_method_chain :gsub, :optional_block
77
- # end
72
+ # gsub: Left alone because of $~, $1, etc... which needs to be "pushed" up one level
73
+ # It's possible to do so, but it's a method that is used everywhere so i felt
74
+ # the performance hit was too big compared to the dubious gain.
78
75
 
79
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
76
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
80
77
  unless method_defined? :each_char
81
78
  def each_char(&block)
82
79
  return to_enum(:each_char) unless block_given?
@@ -86,31 +83,31 @@ class String
86
83
  alias_method :chars, :each_char unless method_defined? :chars
87
84
  end
88
85
 
86
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
89
87
  alias_method :each_codepoint, :codepoints unless method_defined? :each_codepoint
90
88
 
91
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
89
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
92
90
  def end_with?(*suffixes)
93
91
  suffixes.each do |suffix|
94
- suffix = suffix.to_s
92
+ next unless suffix.respond_to? :to_str
93
+ suffix = suffix.to_str
95
94
  return true if self[-suffix.length, suffix.length] == suffix
96
95
  end
97
96
  false
98
97
  end unless method_defined? :end_with?
99
98
 
100
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
99
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
101
100
  def getbyte(i)
102
101
  self[i]
103
102
  end unless method_defined? :getbyte
104
103
 
105
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
104
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
106
105
  unless ("check partition".partition(" ") rescue false)
107
- def partition_with_new_meaning(*args, &block)
108
- return partition_without_new_meaning(*args, &block) unless args.length == 1
109
- pattern = args.first
110
-
106
+ def partition_with_new_meaning(pattern = Undefined, &block)
107
+ return partition_without_new_meaning(&block) if pattern == Undefined
108
+ pattern = Type.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
111
109
  i = index(pattern)
112
110
  return [self, "", ""] unless i
113
-
114
111
  if pattern.is_a? Regexp
115
112
  match = Regexp.last_match
116
113
  [match.pre_match, match[0], match.post_match]
@@ -122,8 +119,9 @@ class String
122
119
  alias_method_chain :partition, :new_meaning
123
120
  end
124
121
 
125
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
122
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
126
123
  def rpartition(pattern)
124
+ pattern = Type.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
127
125
  i = rindex(pattern)
128
126
  return ["", "", self] unless i
129
127
 
@@ -137,10 +135,11 @@ class String
137
135
  end unless method_defined? :rpartition
138
136
 
139
137
 
140
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
138
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
141
139
  def start_with?(*prefixes)
142
140
  prefixes.each do |prefix|
143
- prefix = prefix.to_s
141
+ next unless prefix.respond_to? :to_str
142
+ prefix = prefix.to_str
144
143
  return true if self[0, prefix.length] == prefix
145
144
  end
146
145
  false
@@ -157,6 +156,7 @@ class String
157
156
 
158
157
  unless ("abc".upto("def", true) rescue false)
159
158
  def upto_with_exclusive(to, excl=false, &block)
159
+ return upto_without_exclusive(to, &block) if block_given? && !excl
160
160
  enum = Range.new(self, to, excl).to_enum
161
161
  return enum unless block_given?
162
162
  enum.each(&block)
@@ -1,5 +1,5 @@
1
1
  class Symbol
2
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Symbol.html]
2
+ # Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Symbol.html]
3
3
  def to_proc
4
4
  Proc.new { |*args| args.shift.__send__(self, *args) }
5
5
  end unless :to_proc.respond_to?(:to_proc)
@@ -99,7 +99,7 @@ class EnumerableTest < Test::Unit::TestCase
99
99
  assert_equal nil, %w{ant bat cat dog}.find_index {|item| item =~ /h/ }
100
100
  end
101
101
 
102
- should "#work for enumerables too" do
102
+ should "work for enumerables too" do
103
103
  assert_equal 69-42, (42..666).find_index(69)
104
104
  end
105
105
  end
@@ -25,5 +25,14 @@ class EnumeratorTest < Test::Unit::TestCase
25
25
  end
26
26
  end
27
27
  end
28
+
29
+ context "#next" do
30
+ should "conform to doc" do
31
+ enum = [10, 20].to_enum
32
+ assert_equal 10, enum.next
33
+ assert_equal 20, enum.next
34
+ assert_raise(StopIteration){ enum.next}
35
+ end
36
+ end
28
37
  end
29
38
  end
data/test/kernel_test.rb CHANGED
@@ -8,14 +8,15 @@ end
8
8
  class KernelTest < Test::Unit::TestCase
9
9
  context "Kernel" do
10
10
  context ".loop" do
11
- should "conform to doc" do
12
- enum1 = [1, 2, 3].to_enum
13
- enum2 = [10, 20].to_enum
11
+ should "catch StopIteration" do
12
+ i = 0
14
13
  r = []
15
14
  loop do
16
- r << enum1.next + enum2.next
15
+ r << i
16
+ i += 1
17
+ raise StopIteration if i > 2
17
18
  end
18
- assert_equal [11,22], r
19
+ assert_equal [0, 1, 2], r
19
20
  end
20
21
  end
21
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backports
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.8
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marc-Andr\xC3\xA9 Lafortune"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-01 00:00:00 -04:00
12
+ date: 2009-05-31 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,27 +23,36 @@ extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README.rdoc
25
25
  files:
26
+ - .document
27
+ - .gitignore
26
28
  - CHANGELOG.rdoc
27
29
  - LICENSE
28
30
  - README.rdoc
29
31
  - Rakefile
30
32
  - VERSION.yml
33
+ - backports.gemspec
31
34
  - lib/backports.rb
35
+ - lib/backports/argf.rb
32
36
  - lib/backports/array.rb
33
37
  - lib/backports/binding.rb
38
+ - lib/backports/core_ext.rb
34
39
  - lib/backports/dir.rb
35
40
  - lib/backports/enumerable.rb
36
41
  - lib/backports/enumerator.rb
42
+ - lib/backports/env.rb
37
43
  - lib/backports/fixnum.rb
44
+ - lib/backports/float.rb
45
+ - lib/backports/gc.rb
38
46
  - lib/backports/hash.rb
39
47
  - lib/backports/integer.rb
40
48
  - lib/backports/io.rb
41
49
  - lib/backports/kernel.rb
42
50
  - lib/backports/method.rb
43
51
  - lib/backports/module.rb
44
- - lib/backports/object.rb
52
+ - lib/backports/numeric.rb
45
53
  - lib/backports/object_space.rb
46
54
  - lib/backports/proc.rb
55
+ - lib/backports/process.rb
47
56
  - lib/backports/range.rb
48
57
  - lib/backports/regexp.rb
49
58
  - lib/backports/string.rb
@@ -1,37 +0,0 @@
1
- require 'enumerator'
2
- # Must be defined outside of Kernel for jruby, see http://jira.codehaus.org/browse/JRUBY-3609
3
- Enumerator = Enumerable::Enumerator unless Kernel.const_defined? :Enumerator # Standard in ruby 1.9
4
-
5
- module Kernel # Did you know that object instance methods are defined in Kernel?
6
-
7
- # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
8
- def try(method_id, *args, &block)
9
- send(method_id, *args, &block) unless self.nil? #todo: check new def
10
- end unless method_defined? :try
11
-
12
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
13
- def tap
14
- yield self
15
- self
16
- end unless method_defined? :tap
17
-
18
- # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
19
- def returning(obj)
20
- yield obj
21
- obj
22
- end unless method_defined? :returning
23
-
24
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
25
- def define_singleton_method(symbol, &block)
26
- class << self
27
- self
28
- end.send(:define_method, symbol, block)
29
- end unless method_defined? :define_singleton_method
30
-
31
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
32
- def instance_exec(*arg, &block)
33
- define_singleton_method(:"temporary method for instance_exec", &block)
34
- send(:"temporary method for instance_exec", *arg)
35
- end unless method_defined? :instance_exec
36
-
37
- end