From ed03d4eaa89a7b4ab09e7f5da76b522d04650daf Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 7 Oct 2014 19:52:12 +0900 Subject: Avoid creating range objects (take II) --- activesupport/lib/active_support/core_ext/array/access.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb index caa499dfa2..45b89d2705 100644 --- a/activesupport/lib/active_support/core_ext/array/access.rb +++ b/activesupport/lib/active_support/core_ext/array/access.rb @@ -20,7 +20,11 @@ class Array # %w( a b c d ).to(-2) # => ["a", "b", "c"] # %w( a b c ).to(-10) # => [] def to(position) - self[0..position] + if position >= 0 + first position + 1 + else + self[0..position] + end end # Equal to self[1]. -- cgit v1.2.3