Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby convention for accessing first/last element in array [closed]

This is a question about conventions. The two sets of commands below return identical results.

a = [1, 2, 3]

a.first  # => 1
a[0]     # => 1

a.last   # => 3
a[-1]    # => 3

Which of these is preferred in Ruby, the explicit index or the functions? Assuming, of course, that this is in code which always accesses the first or last element.

Note: I've been thinking about the cycles each would take. Because first and last accept parameters, they will have a little more overhead, but I don't know if that affects what the community prefers.

Thanks!


EDIT

If you read the comments on this post, there was a big debate about my last paragraph. While I failed to remember that [x] is equivalent to .[](x), I was correct in my conclusion that first and last have a bit more overhead. Considering the nature of both, I believe that this is due to the argument check for first/last. These need to check if there are arguments whereas [] can assume that they exist.

CODE

require 'benchmark'

a = [1..1000]

MAX = 1000000

Benchmark.bm(15) do |b|
  b.report("small first") { MAX.times do; a.first; end }
  b.report("small [0]")   { MAX.times do; a[0];    end }
  b.report("small last")  { MAX.times do; a.last;  end }
  b.report("small [-1]")  { MAX.times do; a[-1];   end }
end

a = [1..100000000000]

Benchmark.bm(15) do |b|
  b.report("large first") { MAX.times do; a.first; end }
  b.report("large [0]")   { MAX.times do; a[0];    end }
  b.report("large last")  { MAX.times do; a.last;  end }
  b.report("large [-1]")  { MAX.times do; a[-1];   end }
end

RESULTS

                      user     system      total        real
small first       0.350000   0.000000   0.350000 (  0.901497)
small [0]         0.330000   0.010000   0.340000 (  0.857786)
small last        0.370000   0.000000   0.370000 (  1.054216)
small [-1]        0.370000   0.000000   0.370000 (  1.137655)
                      user     system      total        real
large first       0.340000   0.010000   0.350000 (  0.897581)
large [0]         0.320000   0.010000   0.330000 (  0.889725)
large last        0.350000   0.000000   0.350000 (  1.071135)
large [-1]        0.380000   0.000000   0.380000 (  1.119587)
like image 997
Dan Grahn Avatar asked Aug 13 '13 14:08

Dan Grahn


People also ask

How do you find the last element of an array in Ruby?

Ruby | Array class last() function last() is a Array class method which returns the last element of the array or the last 'n' elements from the array. The first form returns nil, If the array is empty .

What does .first mean in Ruby?

Ruby | Array class first() function first() is a Array class method which returns the first element of the array or the first 'n' elements from the array.

How do you access the elements of an array in Ruby?

The at() method of an array in Ruby is used to access elements of an array. It accepts an integer value and returns the element. This element is the one in which the index position is the value passed in.

How do you pop the first element of an array in Ruby?

Removing the first element If you don't want to modify the original array, you can use the drop() method in Ruby. In the above example, we have passed 1 as an argument to the drop() method. so it creates a new array by removing the first element from the prices array.


2 Answers

Code is read more than it is written, and first and last take less effort to understand, especially for a less experienced Ruby programmer or someone from a language with different indexing semantics.

While most programmers will immediately know that these are the same:

a.first
a[0]

the first still reads more easily. There isn't a marked difference in how hard it is to read, but it's there.

last is another issue. Accessing the index 0 will get you the first element of an array in almost any language. But negative indexing is only available in some languages. If a C programmer with minimal Ruby experience is trying to read my code, which will they understand faster?:

a.last
a[-1]

The negative index will probably force them to do a Google search.

like image 179
Linuxios Avatar answered Oct 26 '22 16:10

Linuxios


Since Matz designed Ruby after a few other languages, I think the conventions come from those other languages.

In Lisp, one of Ruby's inspirational parents, you would use something close to the last and first methods so I'll say last and first is convention.

I only really use first and last. I see many programs out there that use those methods but ultimately it is your choice. That's the beauty of Ruby ;)

like image 43
benbot Avatar answered Oct 26 '22 14:10

benbot