Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Array#from and Array#to that behaves just from String#from and …

…String#to [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8224 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 4d177ae0d6d9f60c4000f45fb6f6df27317afbff 1 parent 6916540
David Heinemeier Hansson dhh authored
2  activesupport/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Added Array#from and Array#to that behaves just from String#from and String#to [DHH]
+
* Fix that empty collections should be treated as empty arrays regardless of whitespace for Hash#from_xml #10255 [adamj]
* Time#time_with_datetime_fallback, Time#to_datetime, Date#to_datetime and String#to_datetime honor Ruby's default calendar reform setting. #10201 [Geoff Buesing]
2  activesupport/lib/active_support/core_ext/array.rb
View
@@ -1,9 +1,11 @@
+require 'active_support/core_ext/array/access'
require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/array/grouping'
require 'active_support/core_ext/array/random_access'
class Array #:nodoc:
+ include ActiveSupport::CoreExtensions::Array::Access
include ActiveSupport::CoreExtensions::Array::Conversions
include ActiveSupport::CoreExtensions::Array::ExtractOptions
include ActiveSupport::CoreExtensions::Array::Grouping
28 activesupport/lib/active_support/core_ext/array/access.rb
View
@@ -0,0 +1,28 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Array #:nodoc:
+ # Makes it easier to access parts of an array.
+ module Access
+ # Returns the remaining of the array from the +position+.
+ #
+ # Examples:
+ # %w( a b c d ).from(0) # => %w( a b c d )
+ # %w( a b c d ).from(2) # => %w( c d )
+ # %w( a b c d ).from(10) # => nil
+ def from(position)
+ self[position..-1]
+ end
+
+ # Returns the beginning of the array up to the +position+.
+ #
+ # Examples:
+ # %w( a b c d ).to(0) # => %w( a )
+ # %w( a b c d ).to(2) # => %w( a b c )
+ # %w( a b c d ).to(10) # => %w( a b c d )
+ def to(position)
+ self[0..position]
+ end
+ end
+ end
+ end
+end
14 activesupport/test/core_ext/array_ext_test.rb
View
@@ -1,6 +1,20 @@
require File.dirname(__FILE__) + '/../abstract_unit'
require 'bigdecimal'
+class ArrayExtAccessTests < Test::Unit::TestCase
+ def test_from
+ assert_equal %w( a b c d ), %w( a b c d ).from(0)
+ assert_equal %w( c d ), %w( a b c d ).from(2)
+ assert_nil %w( a b c d ).from(10)
+ end
+
+ def test_to
+ assert_equal %w( a ), %w( a b c d ).to(0)
+ assert_equal %w( a b c ), %w( a b c d ).to(2)
+ assert_equal %w( a b c d ), %w( a b c d ).to(10)
+ end
+end
+
class ArrayExtToParamTests < Test::Unit::TestCase
def test_string_array
assert_equal '', %w().to_param

2 comments on commit 4d177ae

Pete Forde

I understand that these functions are designed to mimic String equivalents, but I think that Array#from should either return [] or raise an exception if it is invoked with an index that is out of bounds.

“pete”.from(4) # => "" “pete”.from(5) # => "" %w( p e t e ).from(4) # => [] %w( p e t e ).from(5) # => nil

I feel like someone using Array#from is going to be expecting an array to be returned; a nil violates the principle of lease surprise.

Michael Koziarski
Owner

Please don’t use comments to raise issues like this, drop us an email on the core list and we can discuss it there, or use lighthouse tickets.

Please sign in to comment.
Something went wrong with that request. Please try again.