I'm just wondering if there's something similar in Ruby or RnR?
我想知道Ruby或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.
这篇文章:为什么列表中没有tail()或head()方法来获取最后一个或第一个元素?关于Java的讨论,但是我对Ruby或RnR中的内容感到疑惑。
Thanks for yr time!
谢谢你的时间!
4 个解决方案
#1
6
Here is one possiblity:
这是一个可能性:
head, *tail = args
It uses the magical Ruby splat operator.
它使用神奇的Ruby splat操作符。
Example:
例子:
[1] pry(main)> args = [1, 2, 3, 4, 5, 6, 7, 8]
=> [1, 2, 3, 4, 5, 6, 7, 8]
[2] pry(main)> head, *tail = args;
[3] pry(main)> head
=> 1
[4] pry(main)> tail
=> [2, 3, 4, 5, 6, 7, 8]
#2
#3
0
If you are thinking of ActiveRecord, you can use first and last:
如果您正在考虑ActiveRecord,您可以使用first和last:
Unit.first(5)
-> Unit Load (5.3ms) SELECT `units`.* FROM `units` LIMIT 5
Unit.last(5)
-> Unit Load (1.7ms) SELECT `units`.* FROM `units` ORDER BY id DESC LIMIT 5
Or, an array (list):
或者,一个数组(清单):
units.first(5) # first 5
units[0,5] # first 5
units[0..4] # first 5
units.last(5) # last 5
units[-5,5] # last 5
units[-5..-1] # last 5
#4
0
I believe there is nothing like that in ruby
core lib or standard lib. But if you like functional style, you can implement one yourself and avoid monkey patching like this:
我相信在ruby core lib或标准lib中没有类似的东西,但是如果你喜欢函数式的风格,你可以自己实现,避免像这样的猴子补丁:
def head(n)
n.to_a[0]
end
def tail(n)
n.to_a[1..-1]
end
to_a
is necessary if you want to support both array and enumerable. So you can call it like this:
如果您想同时支持数组和可枚举,那么to_a是必需的。所以你可以这样称呼它:
>> head [1, 2, 3, 4, 5]
=> 1
>> tail [1, 2, 3, 4, 5]
=> [2, 3, 4, 5]
>> head 1..10
=> 1
>> tail 1..10
=> [2, 3, 4, 5, 6, 7, 8, 9, 10]
#1
6
Here is one possiblity:
这是一个可能性:
head, *tail = args
It uses the magical Ruby splat operator.
它使用神奇的Ruby splat操作符。
Example:
例子:
[1] pry(main)> args = [1, 2, 3, 4, 5, 6, 7, 8]
=> [1, 2, 3, 4, 5, 6, 7, 8]
[2] pry(main)> head, *tail = args;
[3] pry(main)> head
=> 1
[4] pry(main)> tail
=> [2, 3, 4, 5, 6, 7, 8]
#2
1
The ruby array supports first and last. Similarly ActiveRecord
exposes the same methods for querying a database.
ruby数组首先支持,最后支持。类似地,ActiveRecord公开查询数据库的相同方法。
#3
0
If you are thinking of ActiveRecord, you can use first and last:
如果您正在考虑ActiveRecord,您可以使用first和last:
Unit.first(5)
-> Unit Load (5.3ms) SELECT `units`.* FROM `units` LIMIT 5
Unit.last(5)
-> Unit Load (1.7ms) SELECT `units`.* FROM `units` ORDER BY id DESC LIMIT 5
Or, an array (list):
或者,一个数组(清单):
units.first(5) # first 5
units[0,5] # first 5
units[0..4] # first 5
units.last(5) # last 5
units[-5,5] # last 5
units[-5..-1] # last 5
#4
0
I believe there is nothing like that in ruby
core lib or standard lib. But if you like functional style, you can implement one yourself and avoid monkey patching like this:
我相信在ruby core lib或标准lib中没有类似的东西,但是如果你喜欢函数式的风格,你可以自己实现,避免像这样的猴子补丁:
def head(n)
n.to_a[0]
end
def tail(n)
n.to_a[1..-1]
end
to_a
is necessary if you want to support both array and enumerable. So you can call it like this:
如果您想同时支持数组和可枚举,那么to_a是必需的。所以你可以这样称呼它:
>> head [1, 2, 3, 4, 5]
=> 1
>> tail [1, 2, 3, 4, 5]
=> [2, 3, 4, 5]
>> head 1..10
=> 1
>> tail 1..10
=> [2, 3, 4, 5, 6, 7, 8, 9, 10]