I have a Ruby method that looks for an array of hashes and returns a subset of this array.
def last_actions(type = 'all') actions = @actions if type == 'run' actions = actions.select {|a| a['type'] == "run" } end return actions end
This works, except when only one action is returned, in which case I do not think that it returns an array with one element, but only the element itself. This becomes problematic later.
What is a good way to ensure that it returns an array of 1 element in this case?
Thank.
selectalways returns an array (except that you are breakinside a block that you do not have). So, no matter what happens in your code, this is not the reason.
select
break
, @actions ( Enumerable), select , . , @actions . , , / @actions.
@actions
, , :
a = [1,2,3] b = 4 [*a] => [1, 2, 3] [*b] => [4]
, :
def last_actions(type = 'all') actions = @actions.dup || [] actions.delete_if {|a| a['type'] != "run" } if type == 'run' actions end
:
a = [1,2,3] b = 4 Array(a) => [1, 2, 3] Array(b) => [4]
Source: https://habr.com/ru/post/1737699/More articles:PDFView printWithInfo: autoRotate: not working - objective-chttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1737695/generate-random-letters-and-numbers-together&usg=ALkJrhj5_vs66tnwurjddwavY_YcK5NgQQCocoa osx PDFView NSPrintOperation PrintPanel not showing page preview - cocoaCan I make all the links lowercase using MediaWiki and use a hyphen instead of an underscore as a word delimiter? - lowercasehttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1737698/how-do-i-write-my-own-desktop-sharing-application-in-java&usg=ALkJrhgrdgfYit086ZFYJ__FD7aYmNisYQChoiceField или CharField в форме django - djangoWhy does the timer not work if we do not create a window? - javaLearning libraries without books or textbooks - librariesIs it possible to select data with the maximum value for a column using criteria in sleep mode? - ormМожно ли отобразить в табличной форме с помощьюив html? - jsonAll Articles