8

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.

Alex D
  • 29,755
  • 7
  • 80
  • 126
neo
  • 4,078
  • 4
  • 25
  • 41

4 Answers4

14

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]
toro2k
  • 19,020
  • 7
  • 64
  • 71
12

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]
falsetru
  • 357,413
  • 63
  • 732
  • 636
2

Here is one more way using Array#values_at :-

a = [1, 2, 3, 4]
a.values_at(1..-1) # => [2, 3, 4]
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
1

If you're actually modifying the values of images_all i.e. explicitly drop the first element permanently, just use shift:

images_all.shift
konsolebox
  • 72,135
  • 12
  • 99
  • 105