如何显示数组的第一个和最后一个元素?

时间:2022-06-09 14:26:58

I want to return the first and last elements of an array.

我想返回数组的第一个和最后一个元素。

def first_and_last(a)
  a.first
  a.last
end

but it's only displays the last element and when I put the plus sign it just adds the elements. I want it to print something like this[1,3]. Any suggestions?

但它只显示最后一个元素,当我加上加号时,它只是添加元素。我想要它打印这样的东西[1,3]。有什么建议么?

5 个解决方案

#1


4  

Another way:

其他方式:

['cat', 'dog', 'pig'].values_at(0,-1)
  #=> ["cat", "pig"] 

#2


3  

It looks like you want to create a new array with two elements which are the first and last of the argument, respectively:

看起来你想创建一个包含两个元素的新数组,这两个元素分别是参数的第一个和最后一个:

def first_and_last(a)
  [a.first, a.last]
end

first_and_last([1,2,3]) # => [1, 3]
first_and_last(1..100) # => [1, 100]

#3


2  

def first_and_last(a)
   return a.first, a.last
end

or

要么

def first_and_last(a)
   return a[0], a[-1]
end

#4


0  

def first_and_last(a)
  return [a.first, a.last]
end

in your code you are not returning any value in ruby if you are not return value then last line after execution return automatically. So you need to return like abobe

在你的代码中,如果你没有返回值,那么你不会在ruby中返回任何值,然后在执行返回后自动返回最后一行。所以你需要像abobe一样回归

#5


0  

If you're using Array, Set, ActiveRecord, or some other collection, then you should do the following:

如果您正在使用Array,Set,ActiveRecord或其他一些集合,那么您应该执行以下操作:

def first_and_last( collection )
  [ collection.first, collection.last ]
end

Note: There's no need of the return keyword because the last executed statement within a Ruby method is what gets returned to the caller of the method. Next, parameters to your method should be intention revealing. That is, it should be easy for others to understand what is it. In this case, it's a collection (i.e. Array) which responds to both first and last messages.

注意:不需要return关键字,因为Ruby方法中最后执行的语句是返回给方法调用者的内容。接下来,您的方法的参数应该是有意识的揭示。也就是说,其他人应该很容易理解它是什么。在这种情况下,它是一个响应第一个和最后一个消息的集合(即数组)。

Think different and code well,

认为不同,编码很好,

-Conrad

-Conrad

#1


4  

Another way:

其他方式:

['cat', 'dog', 'pig'].values_at(0,-1)
  #=> ["cat", "pig"] 

#2


3  

It looks like you want to create a new array with two elements which are the first and last of the argument, respectively:

看起来你想创建一个包含两个元素的新数组,这两个元素分别是参数的第一个和最后一个:

def first_and_last(a)
  [a.first, a.last]
end

first_and_last([1,2,3]) # => [1, 3]
first_and_last(1..100) # => [1, 100]

#3


2  

def first_and_last(a)
   return a.first, a.last
end

or

要么

def first_and_last(a)
   return a[0], a[-1]
end

#4


0  

def first_and_last(a)
  return [a.first, a.last]
end

in your code you are not returning any value in ruby if you are not return value then last line after execution return automatically. So you need to return like abobe

在你的代码中,如果你没有返回值,那么你不会在ruby中返回任何值,然后在执行返回后自动返回最后一行。所以你需要像abobe一样回归

#5


0  

If you're using Array, Set, ActiveRecord, or some other collection, then you should do the following:

如果您正在使用Array,Set,ActiveRecord或其他一些集合,那么您应该执行以下操作:

def first_and_last( collection )
  [ collection.first, collection.last ]
end

Note: There's no need of the return keyword because the last executed statement within a Ruby method is what gets returned to the caller of the method. Next, parameters to your method should be intention revealing. That is, it should be easy for others to understand what is it. In this case, it's a collection (i.e. Array) which responds to both first and last messages.

注意:不需要return关键字,因为Ruby方法中最后执行的语句是返回给方法调用者的内容。接下来,您的方法的参数应该是有意识的揭示。也就是说,其他人应该很容易理解它是什么。在这种情况下,它是一个响应第一个和最后一个消息的集合(即数组)。

Think different and code well,

认为不同,编码很好,

-Conrad

-Conrad