All but last element of Ruby array

Perhaps…

a = t               # => [1, 2, 3, 4]
a.first a.size - 1  # => [1, 2, 3]

or

a.take 3

or

a.first 3

or

a.pop

which will return the last and leave the array with everything before it

or make the computer work for its dinner:

a.reverse.drop(1).reverse

or

class Array
  def clip n=1
    take size - n
  end
end
a          # => [1, 2, 3, 4]
a.clip     # => [1, 2, 3]
a = a + a  # => [1, 2, 3, 4, 1, 2, 3, 4]
a.clip 2   # => [1, 2, 3, 4, 1, 2]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)