Module: Polyfill::V2_4::String::Instance::Lines::Method

Defined in:
lib/polyfill/v2_4/string/instance/lines.rb

Instance Method Summary collapse

Instance Method Details

#lines(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/polyfill/v2_4/string/instance/lines.rb', line 9

def lines(*args)
  hash, others = args.partition { |arg| arg.is_a?(::Hash) }
  chomps = hash[0] && hash[0][:chomp]

  unless block_given?
    lines = super(*others)

    if chomps
      separator = others.find do |other|
        other.respond_to?(:to_str)
      end || $INPUT_RECORD_SEPARATOR

      lines.each { |line| line.chomp!(separator) }
    end

    return lines
  end

  block =
    if chomps
      separator = others.find do |other|
        other.respond_to?(:to_str)
      end || $INPUT_RECORD_SEPARATOR

      proc do |line|
        yield(line.chomp(separator))
      end
    else
      Proc.new
    end

  super(*others, &block)
end