Module: Polyfill::V2_4::String

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#casecmp?(other) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/polyfill/v2_4/string.rb', line 15

def casecmp?(other)
  casecmp(InternalUtils.to_str(other)) == 0
end

#concat(*others) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/polyfill/v2_4/string.rb', line 19

def concat(*others)
  return super if others.length == 1

  acc = '' << self
  others.each do |other|
    acc << other
  end

  replace(acc)
end

#each_line(*args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/polyfill/v2_4/string.rb', line 30

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

#lines(*args) ⇒ Object



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
90
91
92
93
94
95
# File 'lib/polyfill/v2_4/string.rb', line 63

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

#match?(pattern, position = 0) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/polyfill/v2_4/string.rb', line 97

def match?(pattern, position = 0)
  !!(self[position..-1] =~ pattern) # rubocop:disable Style/InverseMethods
end

#prepend(*others) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/polyfill/v2_4/string.rb', line 101

def prepend(*others)
  return super if others.length == 1

  acc = '' << self
  others.reverse_each do |other|
    acc.prepend(other)
  end

  replace(acc)
end

#unpack1(*args) ⇒ Object



112
113
114
# File 'lib/polyfill/v2_4/string.rb', line 112

def unpack1(*args)
  unpack(*args).first
end