Module: Polyfill::V2_4::IO

Defined in:
lib/polyfill/v2_4/io.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#each_line(*args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/polyfill/v2_4/io.rb', line 58

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

  unless block_given?
    return super(*others) unless chomps

    separator = others.find do |other|
      other.respond_to?(:to_str)
    end || $INPUT_RECORD_SEPARATOR
    return ::Enumerator.new do |yielder|
      super(*others) do |line|
        yielder.yield(line.chomp(separator))
      end
    end
  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

#gets(*args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/polyfill/v2_4/io.rb', line 91

def gets(*args)
  hash, others = args.partition { |arg| arg.is_a?(::Hash) }

  input = super(*others)

  if !input.nil? && hash[0] && hash[0][:chomp]
    separator = others.find do |other|
      other.respond_to?(:to_str)
    end || $INPUT_RECORD_SEPARATOR

    input.chomp!(separator)
  end

  input
end

#lines(*args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/polyfill/v2_4/io.rb', line 107

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

  unless block_given?
    return super(*others) unless chomps

    separator = others.find do |other|
      other.respond_to?(:to_str)
    end || $INPUT_RECORD_SEPARATOR
    return ::Enumerator.new do |yielder|
      super(*others) do |line|
        yielder.yield(line.chomp(separator))
      end
    end
  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

#readline(*args) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/polyfill/v2_4/io.rb', line 140

def readline(*args)
  hash, others = args.partition { |arg| arg.is_a?(::Hash) }

  input = super(*others)

  if hash[0] && hash[0][:chomp]
    separator = others.find do |other|
      other.respond_to?(:to_str)
    end || $INPUT_RECORD_SEPARATOR

    input.chomp!(separator)
  end

  input
end

#readlines(*args) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/polyfill/v2_4/io.rb', line 156

def readlines(*args)
  hash, others = args.partition { |arg| arg.is_a?(::Hash) }

  inputs = super(*others)

  if hash[0] && hash[0][:chomp]
    separator = others.find do |other|
      other.respond_to?(:to_str)
    end || $INPUT_RECORD_SEPARATOR

    inputs.each { |input| input.chomp!(separator) }
  end

  inputs
end