Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Im wondering if there 's Head- and Tail-methods in Ruby or Ruby on Rails (framework)

I'm just wondering if there's something similar in Ruby or RnR?

This post: Why no tail() or head() method in List to get last or first element? talks about this about Java but I'm wondering the same thing but in Ruby or RnR.

Thanks for yr time!

like image 516
YoniGeek Avatar asked Mar 17 '14 15:03

YoniGeek


People also ask

Is Ruby on Rails a framework?

Ruby on Rails is an open-source web development framework, which provides Ruby developers a timesaving alternative to develop code.

What is .first 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.

What is a Rails application?

Rails is a web application development framework written in the Ruby programming language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks.


1 Answers

Here is one possiblity:

head, *tail = args

It uses the Ruby splat operator.

Example:

args = [1, 2, 3, 4, 5, 6, 7, 8]
# => [1, 2, 3, 4, 5, 6, 7, 8]
head, *tail = args
head
# => 1
tail
# => [2, 3, 4, 5, 6, 7, 8]
like image 84
tolgap Avatar answered Sep 24 '22 09:09

tolgap