Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby array.select multiline block

Tags:

ruby

I am filtering an array in ruby and using a .select block to do so. The conditions are sufficiently complex that a single line block is hideous but not that large so a separate method seems like overkill. Thus I want to use a multiline block. However I am unsure of the syntax.

filtered_array = base_array.select do |elem|     return false if condition1     return false if condition2     return true end 

The above is clearly incorrect as return exits the method, not the block but gives an idea of what I am looking for.

I could also use multiple select statements but that seems to obfuscate what I am trying to do. Note that the above conditions are sufficiently complex that using logical operators to bind them results in a mess.

like image 585
AntonDelprado Avatar asked Jun 14 '12 02:06

AntonDelprado


1 Answers

What you want is next instead of return.

like image 72
rcrogers Avatar answered Sep 20 '22 15:09

rcrogers