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,003
  • 5
  • 77
  • 124
neo
  • 3,908
  • 4
  • 21
  • 38

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
  • 18,715
  • 7
  • 60
  • 69
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
  • 336,967
  • 57
  • 673
  • 597
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
  • 66,700
  • 11
  • 93
  • 101
1

Here is one more way using Array#values_at :-

a = [1, 2, 3, 4]
a.values_at(1..-1) # => [2, 3, 4]
Arup Rakshit
  • 113,563
  • 27
  • 250
  • 306