你会如何在ruby中实现这个成语?

时间:2022-12-09 10:05:56

As someone who came from Java background and being a newbie to Ruby, I was wondering if there is a simple way of doing this with ruby.

作为一名来自Java背景并成为Ruby新手的人,我想知道是否有一种简单的方法可以使用ruby。

new_values = foo(bar)
if new_values
  if arr
    arr << new_values
  else 
    arr = new_values
  end
end

5 个解决方案

#1


7  

Assuming "arr" is either an array or nil, I would use:

假设“arr”是数组或者是nil,我会使用:

arr ||= []
arr << new_values

If you're doing this in a loop or some such, there might be more idiomatic ways to do it. For example, if you're iterating a list, passing each value to foo(), and constructing an array of results, you could just use:

如果你是在循环或其他一些循环中这样做,可能会有更多的惯用方法来做到这一点。例如,如果您正在迭代列表,将每个值传递给foo(),并构造结果数组,您可以使用:

arr = bars.map {|bar| foo(bar) }

#2


3  

If I'm understanding you correctly, I would probably do:

如果我正确地理解你,我可能会这样做:

# Start with an empty array if it hasn't already been set
@arr ||= []

# Add the values to the array as elements
@arr.concat foo(bar)

If you use @arr << values you are adding the entire array of values to the end of the array as a single nested entry.

如果使用@arr < <值,则将整个值数组作为单个嵌套条目添加到数组的末尾。< p>

#3


3  

arr = [*arr.to_a + [*new_values.to_a]]


Start with:

从...开始:

arr ||= []

And then, depending on whether new_values is an array or not

然后,取决于new_values是否是数组

arr += new_values    # if array
arr << new_values    # if not
arr += [*new_values] # if it could be either

Furthermore, you can get rid of the test on new_values by taking advantage of the fact that NilClass implements a .to_a => [] method and reduce everything to:

此外,您可以通过利用NilClass实现.to_a => []方法并将所有内容减少到以下的事实来摆脱对new_values的测试:

arry ||= []
arr += [*new_values.to_a]

But wait, we can use that trick again and make the entire thing into a one-liner:

但是等等,我们可以再次使用这个技巧并将整个事情变成一个单行:


arr = [*arr.to_a + [*new_values.to_a]]

#4


1  

I don't intend to write an inexcrutable one-liner, but I think this is quite clear. Assuming, as Phrogz, that what you really need is an extend (concat):

我不打算写一个不可理喻的单行,但我认为这很清楚。假设,作为Phrogz,你真正需要的是一个扩展(concat):

arr = (arr || []).concat(foo(bar) || [])

Or:

要么:

(arr ||= []).concat(foo(bar) || [])

#5


0  

I would use:

我会用:

new_values = foo(bar)
arr ||= []
arr << new_values if new_values

#1


7  

Assuming "arr" is either an array or nil, I would use:

假设“arr”是数组或者是nil,我会使用:

arr ||= []
arr << new_values

If you're doing this in a loop or some such, there might be more idiomatic ways to do it. For example, if you're iterating a list, passing each value to foo(), and constructing an array of results, you could just use:

如果你是在循环或其他一些循环中这样做,可能会有更多的惯用方法来做到这一点。例如,如果您正在迭代列表,将每个值传递给foo(),并构造结果数组,您可以使用:

arr = bars.map {|bar| foo(bar) }

#2


3  

If I'm understanding you correctly, I would probably do:

如果我正确地理解你,我可能会这样做:

# Start with an empty array if it hasn't already been set
@arr ||= []

# Add the values to the array as elements
@arr.concat foo(bar)

If you use @arr << values you are adding the entire array of values to the end of the array as a single nested entry.

如果使用@arr < <值,则将整个值数组作为单个嵌套条目添加到数组的末尾。< p>

#3


3  

arr = [*arr.to_a + [*new_values.to_a]]


Start with:

从...开始:

arr ||= []

And then, depending on whether new_values is an array or not

然后,取决于new_values是否是数组

arr += new_values    # if array
arr << new_values    # if not
arr += [*new_values] # if it could be either

Furthermore, you can get rid of the test on new_values by taking advantage of the fact that NilClass implements a .to_a => [] method and reduce everything to:

此外,您可以通过利用NilClass实现.to_a => []方法并将所有内容减少到以下的事实来摆脱对new_values的测试:

arry ||= []
arr += [*new_values.to_a]

But wait, we can use that trick again and make the entire thing into a one-liner:

但是等等,我们可以再次使用这个技巧并将整个事情变成一个单行:


arr = [*arr.to_a + [*new_values.to_a]]

#4


1  

I don't intend to write an inexcrutable one-liner, but I think this is quite clear. Assuming, as Phrogz, that what you really need is an extend (concat):

我不打算写一个不可理喻的单行,但我认为这很清楚。假设,作为Phrogz,你真正需要的是一个扩展(concat):

arr = (arr || []).concat(foo(bar) || [])

Or:

要么:

(arr ||= []).concat(foo(bar) || [])

#5


0  

I would use:

我会用:

new_values = foo(bar)
arr ||= []
arr << new_values if new_values