activesupport 3.0.0.rc → 3.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- data/CHANGELOG +42 -37
- data/lib/active_support/base64.rb +3 -3
- data/lib/active_support/benchmarkable.rb +3 -3
- data/lib/active_support/cache.rb +46 -56
- data/lib/active_support/cache/mem_cache_store.rb +3 -4
- data/lib/active_support/cache/memory_store.rb +5 -5
- data/lib/active_support/cache/strategy/local_cache.rb +5 -5
- data/lib/active_support/callbacks.rb +13 -3
- data/lib/active_support/concern.rb +35 -0
- data/lib/active_support/configurable.rb +2 -2
- data/lib/active_support/core_ext/array/conversions.rb +6 -6
- data/lib/active_support/core_ext/array/random_access.rb +4 -4
- data/lib/active_support/core_ext/array/uniq_by.rb +1 -1
- data/lib/active_support/core_ext/array/wrap.rb +30 -4
- data/lib/active_support/core_ext/class/attribute.rb +27 -4
- data/lib/active_support/core_ext/class/attribute_accessors.rb +16 -0
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +25 -3
- data/lib/active_support/core_ext/date/calculations.rb +15 -15
- data/lib/active_support/core_ext/date_time/conversions.rb +6 -6
- data/lib/active_support/core_ext/date_time/zones.rb +1 -1
- data/lib/active_support/core_ext/enumerable.rb +6 -5
- data/lib/active_support/core_ext/hash/conversions.rb +11 -11
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -3
- data/lib/active_support/core_ext/integer/time.rb +2 -2
- data/lib/active_support/core_ext/kernel/reporting.rb +1 -1
- data/lib/active_support/core_ext/module/anonymous.rb +1 -1
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +2 -2
- data/lib/active_support/core_ext/module/attribute_accessors.rb +1 -1
- data/lib/active_support/core_ext/module/remove_method.rb +1 -1
- data/lib/active_support/core_ext/module/synchronization.rb +2 -2
- data/lib/active_support/core_ext/numeric/time.rb +9 -9
- data/lib/active_support/core_ext/object.rb +1 -0
- data/lib/active_support/core_ext/object/blank.rb +1 -1
- data/lib/active_support/core_ext/object/instance_variables.rb +5 -5
- data/lib/active_support/core_ext/object/returning.rb +43 -0
- data/lib/active_support/core_ext/range/conversions.rb +1 -1
- data/lib/active_support/core_ext/string/access.rb +7 -7
- data/lib/active_support/core_ext/string/inflections.rb +8 -6
- data/lib/active_support/core_ext/string/multibyte.rb +5 -5
- data/lib/active_support/core_ext/time/calculations.rb +2 -2
- data/lib/active_support/core_ext/time/conversions.rb +1 -1
- data/lib/active_support/core_ext/time/zones.rb +3 -3
- data/lib/active_support/dependencies.rb +56 -38
- data/lib/active_support/duration.rb +1 -1
- data/lib/active_support/hash_with_indifferent_access.rb +9 -3
- data/lib/active_support/i18n_railtie.rb +1 -1
- data/lib/active_support/lazy_load_hooks.rb +20 -1
- data/lib/active_support/locale/en.yml +3 -3
- data/lib/active_support/log_subscriber.rb +32 -33
- data/lib/active_support/log_subscriber/test_helper.rb +1 -1
- data/lib/active_support/message_encryptor.rb +17 -17
- data/lib/active_support/message_verifier.rb +7 -7
- data/lib/active_support/multibyte.rb +1 -1
- data/lib/active_support/multibyte/chars.rb +37 -36
- data/lib/active_support/multibyte/unicode.rb +4 -4
- data/lib/active_support/notifications.rb +3 -3
- data/lib/active_support/secure_random.rb +13 -13
- data/lib/active_support/testing/assertions.rb +8 -6
- data/lib/active_support/testing/declarative.rb +4 -4
- data/lib/active_support/testing/isolation.rb +1 -1
- data/lib/active_support/testing/pending.rb +3 -3
- data/lib/active_support/testing/performance.rb +9 -5
- data/lib/active_support/time_with_zone.rb +2 -2
- data/lib/active_support/version.rb +1 -1
- data/lib/active_support/xml_mini/nokogiri.rb +6 -1
- data/lib/active_support/xml_mini/nokogirisax.rb +7 -2
- data/lib/active_support/xml_mini/rexml.rb +1 -1
- metadata +5 -4
@@ -9,12 +9,12 @@ class DateTime
|
|
9
9
|
remove_method :to_time if instance_methods.include?(:to_time)
|
10
10
|
|
11
11
|
# Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# This method is aliased to <tt>to_s</tt>.
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# === Examples
|
16
16
|
# datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
|
17
|
-
#
|
17
|
+
#
|
18
18
|
# datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
|
19
19
|
# datetime.to_s(:db) # => "2007-12-04 00:00:00"
|
20
20
|
# datetime.to_s(:number) # => "20071204000000"
|
@@ -50,7 +50,7 @@ class DateTime
|
|
50
50
|
def formatted_offset(colon = true, alternate_utc_string = nil)
|
51
51
|
utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000"
|
55
55
|
def readable_inspect
|
56
56
|
to_s(:rfc822)
|
@@ -83,8 +83,8 @@ class DateTime
|
|
83
83
|
def xmlschema
|
84
84
|
strftime("%Y-%m-%dT%H:%M:%S%Z")
|
85
85
|
end unless instance_methods(false).include?(:xmlschema)
|
86
|
-
|
87
|
-
# Converts self to a floating-point number of seconds since the Unix epoch
|
86
|
+
|
87
|
+
# Converts self to a floating-point number of seconds since the Unix epoch
|
88
88
|
def to_f
|
89
89
|
seconds_since_unix_epoch.to_f
|
90
90
|
end
|
@@ -9,7 +9,7 @@ class DateTime
|
|
9
9
|
# This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
|
10
10
|
# instead of the operating system's time zone.
|
11
11
|
#
|
12
|
-
# You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
|
12
|
+
# You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
|
13
13
|
# and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
|
14
14
|
#
|
15
15
|
# DateTime.new(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
|
@@ -9,7 +9,7 @@ module Enumerable
|
|
9
9
|
#
|
10
10
|
# Example:
|
11
11
|
#
|
12
|
-
# latest_transcripts.group_by(&:day).each do |day, transcripts|
|
12
|
+
# latest_transcripts.group_by(&:day).each do |day, transcripts|
|
13
13
|
# p "#{day} -> #{transcripts.map(&:class).join(', ')}"
|
14
14
|
# end
|
15
15
|
# "2006-03-01 -> Transcript"
|
@@ -66,7 +66,8 @@ module Enumerable
|
|
66
66
|
# +memo+ to the block. Handy for building up hashes or
|
67
67
|
# reducing collections down to one object. Examples:
|
68
68
|
#
|
69
|
-
# %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
|
69
|
+
# %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
|
70
|
+
# # => {'foo' => 'FOO', 'bar' => 'BAR'}
|
70
71
|
#
|
71
72
|
# *Note* that you can't use immutable objects like numbers, true or false as
|
72
73
|
# the memo. You would think the following returns 120, but since the memo is
|
@@ -87,21 +88,21 @@ module Enumerable
|
|
87
88
|
# => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
|
88
89
|
# people.index_by { |person| "#{person.first_name} #{person.last_name}" }
|
89
90
|
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
|
90
|
-
#
|
91
|
+
#
|
91
92
|
def index_by
|
92
93
|
inject({}) do |accum, elem|
|
93
94
|
accum[yield(elem)] = elem
|
94
95
|
accum
|
95
96
|
end
|
96
97
|
end
|
97
|
-
|
98
|
+
|
98
99
|
# Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1.
|
99
100
|
# Works with a block too ala any?, so people.many? { |p| p.age > 26 } # => returns true if more than 1 person is over 26.
|
100
101
|
def many?(&block)
|
101
102
|
size = block_given? ? select(&block).size : self.size
|
102
103
|
size > 1
|
103
104
|
end
|
104
|
-
|
105
|
+
|
105
106
|
# The negative of the Enumerable#include?. Returns true if the collection does not include the object.
|
106
107
|
def exclude?(object)
|
107
108
|
!include?(object)
|
@@ -7,7 +7,7 @@ require 'active_support/core_ext/string/inflections'
|
|
7
7
|
|
8
8
|
class Hash
|
9
9
|
# Returns a string containing an XML representation of its receiver:
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# {"foo" => 1, "bar" => 2}.to_xml
|
12
12
|
# # =>
|
13
13
|
# # <?xml version="1.0" encoding="UTF-8"?>
|
@@ -15,22 +15,22 @@ class Hash
|
|
15
15
|
# # <foo type="integer">1</foo>
|
16
16
|
# # <bar type="integer">2</bar>
|
17
17
|
# # </hash>
|
18
|
-
#
|
18
|
+
#
|
19
19
|
# To do so, the method loops over the pairs and builds nodes that depend on
|
20
20
|
# the _values_. Given a pair +key+, +value+:
|
21
|
-
#
|
21
|
+
#
|
22
22
|
# * If +value+ is a hash there's a recursive call with +key+ as <tt>:root</tt>.
|
23
|
-
#
|
23
|
+
#
|
24
24
|
# * If +value+ is an array there's a recursive call with +key+ as <tt>:root</tt>,
|
25
25
|
# and +key+ singularized as <tt>:children</tt>.
|
26
|
-
#
|
26
|
+
#
|
27
27
|
# * If +value+ is a callable object it must expect one or two arguments. Depending
|
28
28
|
# on the arity, the callable is invoked with the +options+ hash as first argument
|
29
29
|
# with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. Its
|
30
30
|
# return value becomes a new node.
|
31
|
-
#
|
31
|
+
#
|
32
32
|
# * If +value+ responds to +to_xml+ the method is invoked with +key+ as <tt>:root</tt>.
|
33
|
-
#
|
33
|
+
#
|
34
34
|
# * Otherwise, a node with +key+ as tag is created with a string representation of
|
35
35
|
# +value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added.
|
36
36
|
# Unless the option <tt>:skip_types</tt> exists and is true, an attribute "type" is
|
@@ -48,9 +48,9 @@ class Hash
|
|
48
48
|
# "DateTime" => "datetime",
|
49
49
|
# "Time" => "datetime"
|
50
50
|
# }
|
51
|
-
#
|
51
|
+
#
|
52
52
|
# By default the root node is "hash", but that's configurable via the <tt>:root</tt> option.
|
53
|
-
#
|
53
|
+
#
|
54
54
|
# The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can
|
55
55
|
# configure your own builder with the <tt>:builder</tt> option. The method also accepts
|
56
56
|
# options like <tt>:dasherize</tt> and friends, they are forwarded to the builder.
|
@@ -108,7 +108,7 @@ class Hash
|
|
108
108
|
# blank or nil parsed values are represented by nil
|
109
109
|
elsif value.blank? || value['nil'] == 'true'
|
110
110
|
nil
|
111
|
-
# If the type is the only element which makes it then
|
111
|
+
# If the type is the only element which makes it then
|
112
112
|
# this still makes the value nil, except if type is
|
113
113
|
# a XML node(where type['value'] is a Hash)
|
114
114
|
elsif value['type'] && value.size == 1 && !value['type'].is_a?(::Hash)
|
@@ -118,7 +118,7 @@ class Hash
|
|
118
118
|
h[k] = typecast_xml_value(v)
|
119
119
|
h
|
120
120
|
end
|
121
|
-
|
121
|
+
|
122
122
|
# Turn { :files => { :file => #<StringIO> } into { :files => #<StringIO> } so it is compatible with
|
123
123
|
# how multipart uploaded files from HTML appear
|
124
124
|
xml_value["file"].is_a?(StringIO) ? xml_value["file"] : xml_value
|
@@ -7,8 +7,6 @@ class Hash
|
|
7
7
|
# {:a => 1}.with_indifferent_access["a"] # => 1
|
8
8
|
#
|
9
9
|
def with_indifferent_access
|
10
|
-
|
11
|
-
hash.default = self.default
|
12
|
-
hash
|
10
|
+
ActiveSupport::HashWithIndifferentAccess.new_from_hash_copying_default(self)
|
13
11
|
end
|
14
12
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class Integer
|
2
2
|
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
3
3
|
#
|
4
|
-
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
|
4
|
+
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
|
5
5
|
# as well as adding or subtracting their results from a Time object. For example:
|
6
6
|
#
|
7
7
|
# # equivalent to Time.now.advance(:months => 1)
|
@@ -12,7 +12,7 @@ class Integer
|
|
12
12
|
#
|
13
13
|
# # equivalent to Time.now.advance(:months => 4, :years => 5)
|
14
14
|
# (4.months + 5.years).from_now
|
15
|
-
#
|
15
|
+
#
|
16
16
|
# While these methods provide precise calculation when used as in the examples above, care
|
17
17
|
# should be taken to note that this is not true if the result of `months', `years', etc is
|
18
18
|
# converted before use:
|
@@ -38,7 +38,7 @@ module Kernel
|
|
38
38
|
# puts 'But this will'
|
39
39
|
def silence_stream(stream)
|
40
40
|
old_stream = stream.dup
|
41
|
-
stream.reopen(
|
41
|
+
stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
|
42
42
|
stream.sync = true
|
43
43
|
yield
|
44
44
|
ensure
|
@@ -2,7 +2,7 @@ class Module
|
|
2
2
|
# Declare an attribute accessor with an initial default return value.
|
3
3
|
#
|
4
4
|
# To give attribute <tt>:age</tt> the initial value <tt>25</tt>:
|
5
|
-
#
|
5
|
+
#
|
6
6
|
# class Person
|
7
7
|
# attr_accessor_with_default :age, 25
|
8
8
|
# end
|
@@ -16,7 +16,7 @@ class Module
|
|
16
16
|
# To give attribute <tt>:element_name</tt> a dynamic default value, evaluated
|
17
17
|
# in scope of self:
|
18
18
|
#
|
19
|
-
# attr_accessor_with_default(:element_name) { name.underscore }
|
19
|
+
# attr_accessor_with_default(:element_name) { name.underscore }
|
20
20
|
#
|
21
21
|
def attr_accessor_with_default(sym, default = nil, &block)
|
22
22
|
raise 'Default value or block required' unless !default.nil? || block
|
@@ -3,9 +3,9 @@ require 'active_support/core_ext/array/extract_options'
|
|
3
3
|
|
4
4
|
class Module
|
5
5
|
# Synchronize access around a method, delegating synchronization to a
|
6
|
-
# particular mutex. A mutex (either a Mutex, or any object that responds to
|
6
|
+
# particular mutex. A mutex (either a Mutex, or any object that responds to
|
7
7
|
# #synchronize and yields to a block) must be provided as a final :with option.
|
8
|
-
# The :with option should be a symbol or string, and can represent a method,
|
8
|
+
# The :with option should be a symbol or string, and can represent a method,
|
9
9
|
# constant, or instance or class variable.
|
10
10
|
# Example:
|
11
11
|
# class SharedCache
|
@@ -3,7 +3,7 @@ require 'active_support/duration'
|
|
3
3
|
class Numeric
|
4
4
|
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
5
5
|
#
|
6
|
-
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
|
6
|
+
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
|
7
7
|
# as well as adding or subtracting their results from a Time object. For example:
|
8
8
|
#
|
9
9
|
# # equivalent to Time.now.advance(:months => 1)
|
@@ -14,7 +14,7 @@ class Numeric
|
|
14
14
|
#
|
15
15
|
# # equivalent to Time.now.advance(:months => 4, :years => 5)
|
16
16
|
# (4.months + 5.years).from_now
|
17
|
-
#
|
17
|
+
#
|
18
18
|
# While these methods provide precise calculation when used as in the examples above, care
|
19
19
|
# should be taken to note that this is not true if the result of `months', `years', etc is
|
20
20
|
# converted before use:
|
@@ -25,8 +25,8 @@ class Numeric
|
|
25
25
|
# # equivalent to 365.25.days.to_f.from_now
|
26
26
|
# 1.year.to_f.from_now
|
27
27
|
#
|
28
|
-
# In such cases, Ruby's core
|
29
|
-
# Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
|
28
|
+
# In such cases, Ruby's core
|
29
|
+
# Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
|
30
30
|
# Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
|
31
31
|
# date and time arithmetic
|
32
32
|
def seconds
|
@@ -37,13 +37,13 @@ class Numeric
|
|
37
37
|
def minutes
|
38
38
|
ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]])
|
39
39
|
end
|
40
|
-
alias :minute :minutes
|
41
|
-
|
40
|
+
alias :minute :minutes
|
41
|
+
|
42
42
|
def hours
|
43
43
|
ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]])
|
44
44
|
end
|
45
45
|
alias :hour :hours
|
46
|
-
|
46
|
+
|
47
47
|
def days
|
48
48
|
ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
|
49
49
|
end
|
@@ -53,12 +53,12 @@ class Numeric
|
|
53
53
|
ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
|
54
54
|
end
|
55
55
|
alias :week :weeks
|
56
|
-
|
56
|
+
|
57
57
|
def fortnights
|
58
58
|
ActiveSupport::Duration.new(self * 2.weeks, [[:days, self * 14]])
|
59
59
|
end
|
60
60
|
alias :fortnight :fortnights
|
61
|
-
|
61
|
+
|
62
62
|
# Reads best without arguments: 10.minutes.ago
|
63
63
|
def ago(time = ::Time.now)
|
64
64
|
time - self
|
@@ -2,6 +2,7 @@ require 'active_support/core_ext/object/acts_like'
|
|
2
2
|
require 'active_support/core_ext/object/blank'
|
3
3
|
require 'active_support/core_ext/object/duplicable'
|
4
4
|
require 'active_support/core_ext/object/try'
|
5
|
+
require 'active_support/core_ext/object/returning'
|
5
6
|
|
6
7
|
require 'active_support/core_ext/object/conversions'
|
7
8
|
require 'active_support/core_ext/object/instance_variables'
|
@@ -7,7 +7,7 @@ class Object
|
|
7
7
|
# @x, @y = x, y
|
8
8
|
# end
|
9
9
|
# end
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
|
12
12
|
def instance_values #:nodoc:
|
13
13
|
instance_variables.inject({}) do |values, name|
|
@@ -24,7 +24,7 @@ class Object
|
|
24
24
|
# @x, @y = x, y
|
25
25
|
# end
|
26
26
|
# end
|
27
|
-
#
|
27
|
+
#
|
28
28
|
# C.new(0, 1).instance_variable_names # => ["@y", "@x"]
|
29
29
|
if RUBY_VERSION >= '1.9'
|
30
30
|
def instance_variable_names
|
@@ -47,15 +47,15 @@ class Object
|
|
47
47
|
# def initialize(x, y, z)
|
48
48
|
# @x, @y, @z = x, y, z
|
49
49
|
# end
|
50
|
-
#
|
50
|
+
#
|
51
51
|
# def protected_instance_variables
|
52
52
|
# %w(@z)
|
53
53
|
# end
|
54
54
|
# end
|
55
|
-
#
|
55
|
+
#
|
56
56
|
# a = C.new(0, 1, 2)
|
57
57
|
# b = C.new(3, 4, 5)
|
58
|
-
#
|
58
|
+
#
|
59
59
|
# a.copy_instance_variables_from(b, [:@y])
|
60
60
|
# # a is now: @x = 3, @y = 1, @z = 2
|
61
61
|
def copy_instance_variables_from(object, exclude = []) #:nodoc:
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Object
|
2
|
+
# Returns +value+ after yielding +value+ to the block. This simplifies the
|
3
|
+
# process of constructing an object, performing work on the object, and then
|
4
|
+
# returning the object from a method. It is a Ruby-ized realization of the K
|
5
|
+
# combinator, courtesy of Mikael Brockman.
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
#
|
9
|
+
# # Without returning
|
10
|
+
# def foo
|
11
|
+
# values = []
|
12
|
+
# values << "bar"
|
13
|
+
# values << "baz"
|
14
|
+
# return values
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# foo # => ['bar', 'baz']
|
18
|
+
#
|
19
|
+
# # returning with a local variable
|
20
|
+
# def foo
|
21
|
+
# returning values = [] do
|
22
|
+
# values << 'bar'
|
23
|
+
# values << 'baz'
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# foo # => ['bar', 'baz']
|
28
|
+
#
|
29
|
+
# # returning with a block argument
|
30
|
+
# def foo
|
31
|
+
# returning [] do |values|
|
32
|
+
# values << 'bar'
|
33
|
+
# values << 'baz'
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# foo # => ['bar', 'baz']
|
38
|
+
def returning(value)
|
39
|
+
ActiveSupport::Deprecation.warn('Object#returning has been deprecated in favor of Object#tap.', caller)
|
40
|
+
yield(value)
|
41
|
+
value
|
42
|
+
end
|
43
|
+
end
|
@@ -4,27 +4,27 @@ class String
|
|
4
4
|
unless '1.9'.respond_to?(:force_encoding)
|
5
5
|
# Returns the character at the +position+ treating the string as an array (where 0 is the first character).
|
6
6
|
#
|
7
|
-
# Examples:
|
7
|
+
# Examples:
|
8
8
|
# "hello".at(0) # => "h"
|
9
9
|
# "hello".at(4) # => "o"
|
10
10
|
# "hello".at(10) # => ERROR if < 1.9, nil in 1.9
|
11
11
|
def at(position)
|
12
12
|
mb_chars[position, 1].to_s
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
# Returns the remaining of the string from the +position+ treating the string as an array (where 0 is the first character).
|
16
16
|
#
|
17
|
-
# Examples:
|
17
|
+
# Examples:
|
18
18
|
# "hello".from(0) # => "hello"
|
19
19
|
# "hello".from(2) # => "llo"
|
20
20
|
# "hello".from(10) # => "" if < 1.9, nil in 1.9
|
21
21
|
def from(position)
|
22
22
|
mb_chars[position..-1].to_s
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# Returns the beginning of the string up to the +position+ treating the string as an array (where 0 is the first character).
|
26
26
|
#
|
27
|
-
# Examples:
|
27
|
+
# Examples:
|
28
28
|
# "hello".to(0) # => "h"
|
29
29
|
# "hello".to(2) # => "hel"
|
30
30
|
# "hello".to(10) # => "hello"
|
@@ -34,7 +34,7 @@ class String
|
|
34
34
|
|
35
35
|
# Returns the first character of the string or the first +limit+ characters.
|
36
36
|
#
|
37
|
-
# Examples:
|
37
|
+
# Examples:
|
38
38
|
# "hello".first # => "h"
|
39
39
|
# "hello".first(2) # => "he"
|
40
40
|
# "hello".first(10) # => "hello"
|
@@ -50,7 +50,7 @@ class String
|
|
50
50
|
|
51
51
|
# Returns the last character of the string or the last +limit+ characters.
|
52
52
|
#
|
53
|
-
# Examples:
|
53
|
+
# Examples:
|
54
54
|
# "hello".last # => "o"
|
55
55
|
# "hello".last(2) # => "lo"
|
56
56
|
# "hello".last(10) # => "hello"
|