How to select array elements in a given range in Ruby?
You can use ranges in the array subscript:
arr[100..200]
How to return a part of an array in Ruby?
Yes, Ruby has very similar array-slicing syntax to Python. Here is the ri
documentation for the array index method:
--------------------------------------------------------------- Array#[]
array[index] -> obj or nil
array[start, length] -> an_array or nil
array[range] -> an_array or nil
array.slice(index) -> obj or nil
array.slice(start, length) -> an_array or nil
array.slice(range) -> an_array or nil
------------------------------------------------------------------------
Element Reference---Returns the element at index, or returns a
subarray starting at start and continuing for length elements, or
returns a subarray specified by range. Negative indices count
backward from the end of the array (-1 is the last element).
Returns nil if the index (or starting index) are out of range.
a = [ "a", "b", "c", "d", "e" ]
a[2] + a[0] + a[1] #=> "cab"
a[6] #=> nil
a[1, 2] #=> [ "b", "c" ]
a[1..3] #=> [ "b", "c", "d" ]
a[4..7] #=> [ "e" ]
a[6..10] #=> nil
a[-3, 3] #=> [ "c", "d", "e" ]
# special cases
a[5] #=> nil
a[6, 1] #=> nil
a[5, 1] #=> []
a[5..10] #=> []
Select value range from an array, including duplicates
Here's a fairly inefficient approach that uses take_while
:
def last_non_dupe(array, count = 2)
result = [ ]
array.reverse.take_while do |n|
result << n
result.uniq.length <= count
end.reverse
end
It can be improved on by using a Set
which is automatically unique:
require 'set'
def last_non_dupe(array, count = 2)
result = Set.new
array.reverse.take_while do |n|
result << n
result.length <= count
end.reverse
end
Where in either case you do:
last_non_dupe([6, 4, 3, 2, 2])
# => [3, 2, 2]
The count
argument can be changed as necessary for longer or shorter lists.
How does ruby handle array range accessing?
This is a known ugly odd corner. Take a look at the examples in rdoc for Array#slice.
This specific issue is listed as a "special case"
a = [ "a", "b", "c", "d", "e" ]
a[2] + a[0] + a[1] #=> "cab"
a[6] #=> nil
a[1, 2] #=> [ "b", "c" ]
a[1..3] #=> [ "b", "c", "d" ]
a[4..7] #=> [ "e" ]
a[6..10] #=> nil
a[-3, 3] #=> [ "c", "d", "e" ]
# special cases
a[5] #=> nil
a[5, 1] #=> []
a[5..10] #=> []
If the start is exactly one item beyond the end of the array, then it will return []
, an empty array. If the start is beyond that, nil
. It's documented, though I'm not sure of the reason for it.
Select an array item using a number range
You could use a proc:
size_store = proc{ |n|
case n
when 70..139
'Large'
when 15..69
'Medium'
when 1..14
'Small'
end
}
# USAGE: size_store[40]
Rails method for checking if a number in a range appears in an array
I would do
(array & range.to_a).present?
or
array.any? { |element| range.cover?(element) }
I would choose a version depending on the size of the range. If the range is small that the first version is probably faster, because it creates the intersection once and doesn't need to check cover
for every single element in the array. Whereas if the range is huge (but the array is small) the second version might be faster, because a few comparisons might be faster that generating an array out of a huge range and building the intersection.
JavaScript Array: get range of items
Use the array.slice(begin [, end])
function.
var a = ['a','b','c','d','e','f','g'];
var sliced = a.slice(0, 3); //will contain ['a', 'b', 'c']
The last index is non-inclusive; to mimic ruby's behavior you have to increment the end
value. So I guess slice
behaves more like a[m...n]
in ruby.
Related Topics
Safe Navigation Equivalent to Rails Try for Hashes
What Does "No Binary Rubies Available" Mean
Use Separate Authentication Model with Devise on Rails
Ruby on Rails: Conditionally Display a Partial
Get Jekyll Configuration Inside Plugin
What Are the Limitations of Opal
What Is the Point of Object#Presence in Rails
How to Store Enum as String to Database in Rails
Ruby - How to Write a New File with Output from Script
In a Sinatra App on Heroku, Session Is Not Shared Across Dynos
Ruby: Remove Whitespace Chars at the Beginning of a String
Machinist VS Factorygirl - Pros and Cons
Sunspot_Rails Gem - " Errno:: Econnrefused (Connection Refused - Connect (2)) "