marcandre-backports 1.7.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/README.rdoc +31 -36
- data/Rakefile +4 -27
- data/VERSION.yml +2 -2
- data/backports.gemspec +52 -32
- data/lib/backports/{argf.rb → 1.8.7/argf.rb} +8 -8
- data/lib/backports/{array.rb → 1.8.7/array.rb} +26 -43
- data/lib/backports/1.8.7/dir.rb +7 -0
- data/lib/backports/{enumerable.rb → 1.8.7/enumerable.rb} +22 -38
- data/lib/backports/1.8.7/enumerator.rb +19 -0
- data/lib/backports/1.8.7/env.rb +3 -0
- data/lib/backports/1.8.7/fixnum.rb +11 -0
- data/lib/backports/1.8.7/float.rb +4 -0
- data/lib/backports/1.8.7/hash.rb +23 -0
- data/lib/backports/1.8.7/integer.rb +23 -0
- data/lib/backports/{io.rb → 1.8.7/io.rb} +8 -7
- data/lib/backports/1.8.7/kernel.rb +39 -0
- data/lib/backports/{method.rb → 1.8.7/method.rb} +8 -8
- data/lib/backports/1.8.7/module.rb +7 -0
- data/lib/backports/1.8.7/numeric.rb +3 -0
- data/lib/backports/1.8.7/object_space.rb +5 -0
- data/lib/backports/1.8.7/range.rb +4 -0
- data/lib/backports/{regexp.rb → 1.8.7/regexp.rb} +2 -1
- data/lib/backports/1.8.7/string.rb +90 -0
- data/lib/backports/1.8.7/struct.rb +3 -0
- data/lib/backports/1.8.7/symbol.rb +6 -0
- data/lib/backports/1.8.7.rb +4 -0
- data/lib/backports/1.9/array.rb +12 -0
- data/lib/backports/1.9/enumerable.rb +8 -0
- data/lib/backports/1.9/enumerator.rb +15 -0
- data/lib/backports/1.9/hash.rb +15 -0
- data/lib/backports/1.9/integer.rb +3 -0
- data/lib/backports/1.9/kernel.rb +11 -0
- data/lib/backports/1.9/string.rb +39 -0
- data/lib/backports/{symbol.rb → 1.9/symbol.rb} +1 -6
- data/lib/backports/1.9.rb +5 -0
- data/lib/backports/rails/array.rb +6 -0
- data/lib/backports/rails/enumerable.rb +12 -0
- data/lib/backports/rails/hash.rb +31 -0
- data/lib/backports/rails/kernel.rb +12 -0
- data/lib/backports/rails/module.rb +6 -0
- data/lib/backports/rails/string.rb +42 -0
- data/lib/backports/rails.rb +4 -0
- data/lib/backports/tools.rb +117 -0
- data/lib/backports.rb +4 -15
- data/test/enumerator_test.rb +3 -3
- data/test/test_helper.rb +1 -3
- metadata +48 -32
- data/lib/backports/core_ext.rb +0 -61
- data/lib/backports/dir.rb +0 -7
- data/lib/backports/enumerator.rb +0 -43
- data/lib/backports/env.rb +0 -3
- data/lib/backports/fixnum.rb +0 -11
- data/lib/backports/float.rb +0 -4
- data/lib/backports/hash.rb +0 -64
- data/lib/backports/integer.rb +0 -25
- data/lib/backports/kernel.rb +0 -55
- data/lib/backports/module.rb +0 -30
- data/lib/backports/numeric.rb +0 -3
- data/lib/backports/object_space.rb +0 -5
- data/lib/backports/range.rb +0 -4
- data/lib/backports/string.rb +0 -167
- data/lib/backports/struct.rb +0 -3
- /data/lib/backports/{binding.rb → 1.8.7/binding.rb} +0 -0
- /data/lib/backports/{gc.rb → 1.8.7/gc.rb} +0 -0
- /data/lib/backports/{proc.rb → 1.8.7/proc.rb} +0 -0
- /data/lib/backports/{process.rb → 1.8.7/process.rb} +0 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
module Enumerable
|
3
|
+
class Enumerator
|
4
|
+
def next
|
5
|
+
require 'generator'
|
6
|
+
@generator ||= Generator.new(self)
|
7
|
+
raise StopIteration unless @generator.next?
|
8
|
+
@generator.next
|
9
|
+
end unless method_defined? :next
|
10
|
+
|
11
|
+
def rewind
|
12
|
+
@object.rewind if @object.respond_to? :rewind
|
13
|
+
require 'generator'
|
14
|
+
@generator ||= Generator.new(self)
|
15
|
+
@generator.rewind
|
16
|
+
self
|
17
|
+
end unless method_defined? :rewind
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Fixnum
|
2
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
3
|
+
def div(n)
|
4
|
+
(self / n).to_i
|
5
|
+
end unless method_defined? :div
|
6
|
+
|
7
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
8
|
+
def fdiv(n)
|
9
|
+
to_f / n
|
10
|
+
end unless method_defined? :fdiv
|
11
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Hash
|
2
|
+
# New Ruby 1.8.7+ constructor -- not documented, see redmine # 1385
|
3
|
+
# <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt>
|
4
|
+
class << self
|
5
|
+
alias_method :original_constructor, :[]
|
6
|
+
def [](*args)
|
7
|
+
return original_constructor(*args) unless args.length == 1 && args.first.is_a?(Array)
|
8
|
+
{}.tap do |h|
|
9
|
+
args.first.each do |arr|
|
10
|
+
next unless arr.respond_to? :to_ary
|
11
|
+
arr = arr.to_ary
|
12
|
+
next unless (1..2).include? arr.size
|
13
|
+
h[arr.at(0)] = arr.at(1)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end unless (Hash[[[:test, :test]]] rescue false)
|
17
|
+
end
|
18
|
+
|
19
|
+
Backports.make_block_optional self, :delete_if, :each, :each_key, :each_pair, :each_value, :reject, :reject!, :select, :test_on => {:hello => "world!"}
|
20
|
+
|
21
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
|
22
|
+
alias_method :key, :index unless method_defined? :key
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Integer
|
2
|
+
Backports.make_block_optional self, :downto, :times, :upto, :test_on => 42, :arg => 42
|
3
|
+
|
4
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
5
|
+
def even?
|
6
|
+
self[0].zero?
|
7
|
+
end unless method_defined? :even?
|
8
|
+
|
9
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
10
|
+
def odd?
|
11
|
+
!even?
|
12
|
+
end unless method_defined? :odd?
|
13
|
+
|
14
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
15
|
+
def ord
|
16
|
+
self
|
17
|
+
end unless method_defined? :ord
|
18
|
+
|
19
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
20
|
+
def pred
|
21
|
+
self - 1
|
22
|
+
end unless method_defined? :pred
|
23
|
+
end
|
@@ -1,22 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
make_block_optional :each, :each_line, :each_byte
|
1
|
+
class IO
|
2
|
+
if RUBY_VERSION < '1.8.7'
|
3
|
+
Backports.make_block_optional self, :each, :each_line, :each_byte
|
4
4
|
class << self
|
5
|
-
make_block_optional :foreach
|
5
|
+
Backports.make_block_optional self, :foreach
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
class IO
|
9
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
12
10
|
def bytes
|
13
11
|
to_enum :each_byte
|
14
12
|
end unless method_defined? :bytes
|
15
13
|
|
14
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
16
15
|
def chars
|
17
16
|
to_enum :each_char
|
18
17
|
end unless method_defined? :chars
|
19
18
|
|
19
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
20
20
|
def each_char
|
21
21
|
return to_enum(:each_char) unless block_given?
|
22
22
|
if $KCODE == "UTF-8"
|
@@ -47,6 +47,7 @@ class IO
|
|
47
47
|
alias_method :getbyte, :getc unless method_defined? :getbyte
|
48
48
|
alias_method :readbyte, :readchar unless method_defined? :readchar
|
49
49
|
|
50
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
50
51
|
def lines(*args)
|
51
52
|
to_enum :each_line, *args
|
52
53
|
end unless method_defined? :lines
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Kernel
|
2
|
+
|
3
|
+
def __method__
|
4
|
+
m = caller(1).first[/`(.*)'/,1]
|
5
|
+
m.to_sym if m
|
6
|
+
end unless (__method__ || true rescue false)
|
7
|
+
|
8
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
9
|
+
def define_singleton_method(*args, &block)
|
10
|
+
class << self
|
11
|
+
self
|
12
|
+
end.send(:define_method, *args, &block)
|
13
|
+
end unless method_defined? :define_singleton_method
|
14
|
+
|
15
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
16
|
+
def instance_exec(*arg, &block)
|
17
|
+
define_singleton_method(:"temporary method for instance_exec", &block)
|
18
|
+
send(:"temporary method for instance_exec", *arg)
|
19
|
+
end unless method_defined? :instance_exec
|
20
|
+
|
21
|
+
# Loop. Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
22
|
+
unless const_defined? :StopIteration
|
23
|
+
class StopIteration < IndexError; end
|
24
|
+
|
25
|
+
def loop_with_stop_iteration(&block)
|
26
|
+
loop_without_stop_iteration(&block)
|
27
|
+
rescue StopIteration
|
28
|
+
# ignore silently
|
29
|
+
end
|
30
|
+
Backports.alias_method_chain self, :loop, :stop_iteration
|
31
|
+
end
|
32
|
+
|
33
|
+
# Standard in ruby 1.8.7. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
34
|
+
def tap
|
35
|
+
yield self
|
36
|
+
self
|
37
|
+
end unless method_defined? :tap
|
38
|
+
|
39
|
+
end
|
@@ -3,45 +3,45 @@ unless Method.method_defined? :name
|
|
3
3
|
attr_accessor :name, :receiver, :owner
|
4
4
|
|
5
5
|
def unbind_with_additional_info
|
6
|
-
|
6
|
+
unbind_without_additional_info.tap do |unbound|
|
7
7
|
unbound.name = name
|
8
8
|
unbound.owner = owner
|
9
9
|
end
|
10
10
|
end
|
11
|
-
alias_method_chain :unbind, :additional_info
|
11
|
+
Backports.alias_method_chain self, :unbind, :additional_info
|
12
12
|
end
|
13
13
|
|
14
14
|
class UnboundMethod
|
15
15
|
attr_accessor :name, :owner
|
16
16
|
|
17
17
|
def bind_with_additional_info(to)
|
18
|
-
|
18
|
+
bind_without_additional_info(to).tap do |bound|
|
19
19
|
bound.name = name
|
20
20
|
bound.owner = owner
|
21
21
|
bound.receiver = to
|
22
22
|
end
|
23
23
|
end
|
24
|
-
alias_method_chain :bind, :additional_info
|
24
|
+
Backports.alias_method_chain self, :bind, :additional_info
|
25
25
|
end
|
26
26
|
|
27
27
|
class Object
|
28
28
|
def method_with_additional_info(name)
|
29
|
-
|
29
|
+
method_without_additional_info(name).tap do |bound|
|
30
30
|
bound.name = name.to_sym
|
31
31
|
bound.receiver = self
|
32
32
|
bound.owner = self.class.ancestors.find{|mod| mod.instance_methods(false).include? name.to_s}
|
33
33
|
end
|
34
34
|
end
|
35
|
-
alias_method_chain :method, :additional_info
|
35
|
+
Backports.alias_method_chain self, :method, :additional_info
|
36
36
|
end
|
37
37
|
|
38
38
|
class Module
|
39
39
|
def instance_method_with_additional_info(name)
|
40
|
-
|
40
|
+
instance_method_without_additional_info(name).tap do |unbound|
|
41
41
|
unbound.name = name.to_sym
|
42
42
|
unbound.owner = ancestors.find{|mod| mod.instance_methods(false).include? name.to_s}
|
43
43
|
end
|
44
44
|
end
|
45
|
-
alias_method_chain :instance_method, :additional_info
|
45
|
+
Backports.alias_method_chain self, :instance_method, :additional_info
|
46
46
|
end
|
47
47
|
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class Module
|
2
|
+
# Can't use alias_method here because of jruby (see http://jira.codehaus.org/browse/JRUBY-2435 )
|
3
|
+
def module_exec(*arg, &block)
|
4
|
+
instance_exec(*arg, &block)
|
5
|
+
end unless method_defined? :module_exec
|
6
|
+
alias_method :class_exec, :module_exec unless method_defined? :class_exec
|
7
|
+
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
class Regexp
|
2
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://www.ruby-doc.org/core-1.8.7/classes/Regexp.html]
|
2
3
|
class << self
|
3
4
|
unless (union(%w(a b)) rescue false)
|
4
5
|
def union_with_array_argument(*arg)
|
5
6
|
return union_without_array_argument(*arg) unless arg.size == 1
|
6
7
|
union_without_array_argument(*arg.first)
|
7
8
|
end
|
8
|
-
alias_method_chain :union, :array_argument
|
9
|
+
Backports.alias_method_chain self, :union, :array_argument
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
class String
|
2
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
3
|
+
alias_method :bytesize, :length unless method_defined? :bytesize
|
4
|
+
|
5
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
6
|
+
def chr
|
7
|
+
chars.first
|
8
|
+
end unless method_defined? :chr
|
9
|
+
|
10
|
+
Backports.make_block_optional self, :each_byte, :each, :each_line, :test_on => "abc"
|
11
|
+
|
12
|
+
# gsub: Left alone because of $~, $1, etc... which needs to be "pushed" up one level
|
13
|
+
# It's possible to do so, but gsub is used everywhere so i felt
|
14
|
+
# the performance hit was too big compared to the dubious gain.
|
15
|
+
|
16
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
17
|
+
unless method_defined? :each_char
|
18
|
+
def each_char(&block)
|
19
|
+
return to_enum(:each_char) unless block_given?
|
20
|
+
scan(/./, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :chars, :each_char unless method_defined? :chars
|
24
|
+
end
|
25
|
+
|
26
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
27
|
+
def end_with?(*suffixes)
|
28
|
+
suffixes.each do |suffix|
|
29
|
+
next unless suffix.respond_to? :to_str
|
30
|
+
suffix = suffix.to_str
|
31
|
+
return true if self[-suffix.length, suffix.length] == suffix
|
32
|
+
end
|
33
|
+
false
|
34
|
+
end unless method_defined? :end_with?
|
35
|
+
|
36
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
37
|
+
unless ("check partition".partition(" ") rescue false)
|
38
|
+
def partition_with_new_meaning(pattern = Backports::Undefined, &block)
|
39
|
+
return partition_without_new_meaning(&block) if pattern == Backports::Undefined
|
40
|
+
pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
|
41
|
+
i = index(pattern)
|
42
|
+
return [self, "", ""] unless i
|
43
|
+
if pattern.is_a? Regexp
|
44
|
+
match = Regexp.last_match
|
45
|
+
[match.pre_match, match[0], match.post_match]
|
46
|
+
else
|
47
|
+
last = i+pattern.length
|
48
|
+
[self[0...i], self[i...last], self[last...length]]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
Backports.alias_method_chain self, :partition, :new_meaning
|
52
|
+
end
|
53
|
+
|
54
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
55
|
+
def rpartition(pattern)
|
56
|
+
pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
|
57
|
+
i = rindex(pattern)
|
58
|
+
return ["", "", self] unless i
|
59
|
+
|
60
|
+
if pattern.is_a? Regexp
|
61
|
+
match = Regexp.last_match
|
62
|
+
[match.pre_match, match[0], match.post_match]
|
63
|
+
else
|
64
|
+
last = i+pattern.length
|
65
|
+
[self[0...i], self[i...last], self[last...length]]
|
66
|
+
end
|
67
|
+
end unless method_defined? :rpartition
|
68
|
+
|
69
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
70
|
+
def start_with?(*prefixes)
|
71
|
+
prefixes.each do |prefix|
|
72
|
+
next unless prefix.respond_to? :to_str
|
73
|
+
prefix = prefix.to_str
|
74
|
+
return true if self[0, prefix.length] == prefix
|
75
|
+
end
|
76
|
+
false
|
77
|
+
end unless method_defined? :start_with?
|
78
|
+
|
79
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
80
|
+
unless ("abc".upto("def", true) rescue false)
|
81
|
+
def upto_with_exclusive(to, excl=false, &block)
|
82
|
+
return upto_without_exclusive(to, &block) if block_given? && !excl
|
83
|
+
enum = Range.new(self, to, excl).to_enum
|
84
|
+
return enum unless block_given?
|
85
|
+
enum.each(&block)
|
86
|
+
self
|
87
|
+
end
|
88
|
+
Backports.alias_method_chain self, :upto, :exclusive
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,4 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/tools")
|
2
|
+
%w(module kernel array enumerable enumerator string symbol integer numeric fixnum hash proc binding dir io method regexp struct float object_space argf gc env process).each do |lib|
|
3
|
+
Backports.require_relative "1.8.7/#{lib}"
|
4
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Array
|
2
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
|
3
|
+
class << self
|
4
|
+
# Try to convert obj into an array, using to_ary method.
|
5
|
+
# Returns converted array or nil if obj cannot be converted
|
6
|
+
# for any reason. This method is to check if an argument is an array.
|
7
|
+
def try_convert(obj)
|
8
|
+
return nil unless obj.respond_to?(:to_ary)
|
9
|
+
Backports.coerce_to(obj, Array, :to_ary)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
3
|
+
def each_with_object(memo, &block)
|
4
|
+
return to_enum(:each_with_object, memo) unless block_given?
|
5
|
+
each {|obj| block.call(obj, memo)}
|
6
|
+
memo
|
7
|
+
end unless method_defined? :each_with_object
|
8
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Must be defined outside of Kernel for jruby, see http://jira.codehaus.org/browse/JRUBY-3609
|
2
|
+
Enumerator = Enumerable::Enumerator unless Kernel.const_defined? :Enumerator # Standard in ruby 1.9
|
3
|
+
|
4
|
+
class Enumerator
|
5
|
+
# new with block, standard in Ruby 1.9
|
6
|
+
unless (self.new{} rescue false)
|
7
|
+
def initialize_with_optional_block(*arg, &block)
|
8
|
+
return initialize_without_optional_block(*arg, &nil) unless arg.empty? # Ruby 1.9 apparently ignores the block if any argument is present
|
9
|
+
initialize_without_optional_block(Backports::Yielder.new(&block))
|
10
|
+
end
|
11
|
+
Backports.alias_method_chain self, :initialize, :optional_block
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_method :with_object, :each_with_object unless method_defined? :with_object
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Hash
|
2
|
+
# New Ruby 1.8.7+ constructor -- not documented, see redmine # 1385
|
3
|
+
# <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt>
|
4
|
+
class << self
|
5
|
+
def try_convert(x)
|
6
|
+
return nil unless x.respond_to? :to_hash
|
7
|
+
x.to_hash
|
8
|
+
end unless method_defined? :to_hash
|
9
|
+
end
|
10
|
+
|
11
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
|
12
|
+
def default_proc=(proc)
|
13
|
+
replace(Hash.new(&Backports.coerce_to(proc, Proc, :to_proc)).merge!(self))
|
14
|
+
end unless method_defined? :default_proc=
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Kernel
|
2
|
+
alias_method :__callee__, :__method__ unless (__callee__ || true rescue false)
|
3
|
+
|
4
|
+
def require_relative(relative_feature)
|
5
|
+
file = caller.first.split(/:\d/,2).first
|
6
|
+
if /\A\((.*)\)/ =~ file # eval, etc.
|
7
|
+
raise LoadError, "require_relative is called in #{$1}"
|
8
|
+
end
|
9
|
+
require File.expand_path(relative_feature, File.dirname(file))
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class String
|
2
|
+
class << self
|
3
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
4
|
+
def try_convert(x)
|
5
|
+
return nil unless x.respond_do(:to_str)
|
6
|
+
x.to_str
|
7
|
+
end unless method_defined? :try_convert
|
8
|
+
end
|
9
|
+
|
10
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
11
|
+
def ascii_only?
|
12
|
+
!(self =~ /[^\x00-\x7f]/)
|
13
|
+
end unless method_defined? :ascii_only?
|
14
|
+
|
15
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
16
|
+
def clear
|
17
|
+
self[0,length] = ""
|
18
|
+
self
|
19
|
+
end unless method_defined? :clear
|
20
|
+
|
21
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
22
|
+
def codepoints
|
23
|
+
return to_enum(:codepoints) unless block_given?
|
24
|
+
each_char.each do |s|
|
25
|
+
utf8 = s.each_byte.to_a
|
26
|
+
utf8[0] &= 0xff >> utf8.size # clear high bits (1 to 4, depending of number of bytes used)
|
27
|
+
yield utf8.inject{|result, b| (result << 6) + (b & 0x3f) }
|
28
|
+
end
|
29
|
+
end unless method_defined? :codepoints
|
30
|
+
|
31
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
32
|
+
alias_method :each_codepoint, :codepoints unless method_defined? :each_codepoint
|
33
|
+
|
34
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
35
|
+
def getbyte(i)
|
36
|
+
self[i]
|
37
|
+
end unless method_defined? :getbyte
|
38
|
+
|
39
|
+
end
|
@@ -1,9 +1,4 @@
|
|
1
1
|
class Symbol
|
2
|
-
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Symbol.html]
|
3
|
-
def to_proc
|
4
|
-
Proc.new { |*args| args.shift.__send__(self, *args) }
|
5
|
-
end unless :to_proc.respond_to?(:to_proc)
|
6
|
-
|
7
2
|
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Symbol.html]
|
8
3
|
[ [%w(capitalize downcase next succ swapcase upcase), {:after => ".to_sym"}],
|
9
4
|
[%w(=~ [] empty? length match size), {}]
|
@@ -34,4 +29,4 @@ class Symbol
|
|
34
29
|
alias_method :==, :dont_override_equal_please
|
35
30
|
end
|
36
31
|
|
37
|
-
end
|
32
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Standard in rails... See official documentation[http://api.rubyonrails.org/classes/Enumerable.html]
|
3
|
+
# Modified from rails 2.3 to not rely on size
|
4
|
+
def sum(identity = 0, &block)
|
5
|
+
if block_given?
|
6
|
+
map(&block).sum(identity)
|
7
|
+
else
|
8
|
+
inject { |sum, element| sum + element } || identity
|
9
|
+
end
|
10
|
+
end unless method_defined? :sum
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Hash
|
2
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
3
|
+
def reverse_merge(other_hash)
|
4
|
+
other_hash.merge(self)
|
5
|
+
end
|
6
|
+
|
7
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
8
|
+
def reverse_merge!(other_hash)
|
9
|
+
replace(reverse_merge(other_hash))
|
10
|
+
end
|
11
|
+
|
12
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
13
|
+
def symbolize_keys
|
14
|
+
Hash[map{|key,value| [(key.to_sym rescue key) || key, value] }]
|
15
|
+
end unless method_defined? :symbolize_keys
|
16
|
+
|
17
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
18
|
+
def symbolize_keys!
|
19
|
+
self.replace(self.symbolize_keys)
|
20
|
+
end unless method_defined? :symbolize_keys!
|
21
|
+
|
22
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
23
|
+
def stringify_keys
|
24
|
+
Hash[map{|key,value| [key.to_s, value] }]
|
25
|
+
end unless method_defined? :stringify_keys
|
26
|
+
|
27
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
28
|
+
def stringify_keys!
|
29
|
+
self.replace(self.stringify_keys)
|
30
|
+
end unless method_defined? :stringify_keys!
|
31
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Kernel
|
2
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
|
3
|
+
def try(method_id, *args, &block)
|
4
|
+
send(method_id, *args, &block) unless self.nil?
|
5
|
+
end unless method_defined? :try
|
6
|
+
|
7
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
|
8
|
+
def returning(obj)
|
9
|
+
yield obj
|
10
|
+
obj
|
11
|
+
end unless method_defined? :returning
|
12
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
class Module
|
2
|
+
# Standard in rails... See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Module.html]
|
3
|
+
def alias_method_chain(target, feature)
|
4
|
+
Backports.alias_method_chain(self, target, feature)
|
5
|
+
end unless method_defined? :alias_method_chain
|
6
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class String
|
2
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
3
|
+
def camelize(first_letter = :upper)
|
4
|
+
if first_letter == :upper
|
5
|
+
gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
6
|
+
else
|
7
|
+
first.downcase + camelize[1..-1]
|
8
|
+
end
|
9
|
+
end unless method_defined? :camelize
|
10
|
+
|
11
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
12
|
+
def constantize
|
13
|
+
names = split('::')
|
14
|
+
names.shift if names.empty? || names.first.empty?
|
15
|
+
|
16
|
+
constant = Object
|
17
|
+
names.each do |name|
|
18
|
+
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
19
|
+
end
|
20
|
+
constant
|
21
|
+
end unless method_defined? :constantize
|
22
|
+
|
23
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
24
|
+
def dasherize
|
25
|
+
gsub(/_/, '-')
|
26
|
+
end unless method_defined? :dasherize
|
27
|
+
|
28
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
29
|
+
def demodulize
|
30
|
+
gsub(/^.*::/, '')
|
31
|
+
end unless method_defined? :demodulize
|
32
|
+
|
33
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
34
|
+
def underscore
|
35
|
+
gsub(/::/, '/').
|
36
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
37
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
38
|
+
tr("-", "_").
|
39
|
+
downcase
|
40
|
+
end unless method_defined? :underscore
|
41
|
+
|
42
|
+
end
|