Method: Enumerable#select_from

Defined in:
lib/quality_extensions/enumerable/select_while.rb

#select_from(inclusive = true) ⇒ Object Also known as: select_all_starting_with, reject_until

Returns all elements of enum starting at the first element for which block is truthy, and continuing until the end.

Examples:

(0..3).select_from {|v| v == 1} # => [1, 2, 3] (0..3).select_from(false) {|v| v == 1} # => [2, 3]

‘b’=>2, ‘c’=>3, ‘d’=>1.select_from {|k, v| v == 2} ) # => “c”=>3, “d”=>1 ‘b’=>2, ‘c’=>3, ‘d’=>1.select_from(false) {|k, v| v == 2} ) # => “d”=>1

Compare to Array#from in ActiveSupport. (maybe should rename to #from?)



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/quality_extensions/enumerable/select_while.rb', line 222

def select_from(inclusive = true)
  return self unless block_given?

  selecting = false
  select do |*args|
    # Once we hit the first truthy result, selecting will be true to the end
    select_this_el = !!yield(*args)
    this_is_first_true = !selecting && select_this_el
    selecting = selecting || select_this_el
    if inclusive
      selecting
    else
      selecting && !this_is_first_true
    end
  end
end