如何在Ruby中添加数组?

时间:2022-03-04 15:58:47

I'm sure this is simple but I can't seem to get it:

我确信这很简单,但我似乎不明白:

Works:

工作原理:

@build1 = Booking.build_booking('2009-06-13',3,2,18314)
@build2 = Booking.build_booking('2009-06-13',3,4,18317)
@build = @build1 + @build2

What I want to work...

我想要的工作……

#for item in @cart.items do
#  @build << Booking.build_booking('2009-06-13',3,2,18314)
#end

Doesn't work either...

要么不工作…

#(1..3).each do |i|
#  @build << Booking.build_booking('2009-06-13',3,2,18314)
#end

5 个解决方案

#1


15  

For the two iterating examples you'd need to set @build prior to calling << on it.

对于两个迭代示例,您需要在调用<< on it之前设置@build。

I'm not sure what build_booking is returning but if it's an array (I'm guessing from the first, working, example) then you'd probably want to add the result of build_booking to @build. E.g.

我不确定build_booking返回的是什么,但是如果它是一个数组(我猜是从第一个例子开始的),那么您可能希望将build_booking的结果添加到@build中。如。

@build = []
for item in @cart.items do
  @build += Booking.build_booking('2009-06-13',3,2,18314)
end

#2


27  

I prefer using the wonderful array-methods that ruby has to offer over a for loop:

我更喜欢使用ruby提供的出色的array-methods而不是for循环:

@build = @cart.items.map { |item| Booking.build_booking('2009-06-13',3,2,18314) }

#3


0  

@build will need to be an array, or an object that responds to <<, for @build << to work.

@build需要是一个数组,或者一个响应<<,用于@build < to work的对象。

When you've done:

当你所做的:

@build = @build1 + @build2

What is the value of @build?

@build的价值是什么?

#4


0  

The quick approach, though, would be to simply declare the array to combine the two elements:

不过,快速的方法是简单地声明数组以合并这两个元素:

@build = [ @build1, @build2 ]

I'd use an approach like Magnar, though, which is much more concise.

不过,我会使用Magnar这样的方法,它要简洁得多。

#5


0  

@build = []
for item in @cart.items do
  @build += Booking.build_booking('2009-06-13',3,2,18314)
end


 @build.flatten!

flatten will will work even Booking.build_booking is returning an array of bookings

平展甚至可以预订。build_booking返回一系列的预订

#1


15  

For the two iterating examples you'd need to set @build prior to calling << on it.

对于两个迭代示例,您需要在调用<< on it之前设置@build。

I'm not sure what build_booking is returning but if it's an array (I'm guessing from the first, working, example) then you'd probably want to add the result of build_booking to @build. E.g.

我不确定build_booking返回的是什么,但是如果它是一个数组(我猜是从第一个例子开始的),那么您可能希望将build_booking的结果添加到@build中。如。

@build = []
for item in @cart.items do
  @build += Booking.build_booking('2009-06-13',3,2,18314)
end

#2


27  

I prefer using the wonderful array-methods that ruby has to offer over a for loop:

我更喜欢使用ruby提供的出色的array-methods而不是for循环:

@build = @cart.items.map { |item| Booking.build_booking('2009-06-13',3,2,18314) }

#3


0  

@build will need to be an array, or an object that responds to <<, for @build << to work.

@build需要是一个数组,或者一个响应<<,用于@build < to work的对象。

When you've done:

当你所做的:

@build = @build1 + @build2

What is the value of @build?

@build的价值是什么?

#4


0  

The quick approach, though, would be to simply declare the array to combine the two elements:

不过,快速的方法是简单地声明数组以合并这两个元素:

@build = [ @build1, @build2 ]

I'd use an approach like Magnar, though, which is much more concise.

不过,我会使用Magnar这样的方法,它要简洁得多。

#5


0  

@build = []
for item in @cart.items do
  @build += Booking.build_booking('2009-06-13',3,2,18314)
end


 @build.flatten!

flatten will will work even Booking.build_booking is returning an array of bookings

平展甚至可以预订。build_booking返回一系列的预订