My method:
def scroll_images
images_all[1..images_all.length]
end
I don't like that I'm calling images_all
twice, just wondering if there's a good trick to call self
or something similar to make this a little cleaner.
My method:
def scroll_images
images_all[1..images_all.length]
end
I don't like that I'm calling images_all
twice, just wondering if there's a good trick to call self
or something similar to make this a little cleaner.
You can get the same result in a clearer way using the Array#drop
method:
a = [1, 2, 3, 4]
a.drop(1)
# => [2, 3, 4]
Use -1
instead of the length:
def scroll_images
images_all[1..-1] # `-1`: the last element, `1..-1`: The second to the last.
end
Example:
a = [1, 2, 3, 4]
a[1..-1]
# => [2, 3, 4]
Here is one more way using Array#values_at
:-
a = [1, 2, 3, 4]
a.values_at(1..-1) # => [2, 3, 4]
If you're actually modifying the values of images_all
i.e. explicitly drop the first element permanently, just use shift
:
images_all.shift