Sha256: b3c62d2b7e9e65f1154d04b83b8035d598e2a8b3e0e6fce0e22394c1d0f007ad

Contents?: true

Size: 1.12 KB

Versions: 14

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

class String
  # Change the case of characters randomly
  # @return [String] the case modified string
  # @example
  #   'SELECT * FROM'.randomcase # => "SElECt * frOm"
  #   'SELECT * FROM'.randomcase # => "selECT * FROm"
  def randomcase
    chars.map { |c| rand(0..1).zero? ? c.downcase : c.upcase }.join
  end

  # Change the case of characters randomly in place as described for
  # {String#randomcase}.
  def randomcase!
    replace(randomcase)
  end

  # Change one characte on two upcase and the other downcase
  # @param shift [Integer] 0: 1st character will be downcase, 1: 1st character
  #   will be upcase
  # @return [String] the case modified string
  # @example
  #   'SELECT * FROM'.alternatecase # => "sElEcT * FrOm"
  #   'SELECT * FROM'.alternatecase(1) # => "SeLeCt * fRoM"
  def alternatecase(shift = 0)
    chars.each_with_index.map { |c, i| (i + shift).even? ? c.downcase : c.upcase }.join
  end

  # Change one characte on two upcase and the other downcase in place as
  # described for {String#alternatecase}.
  def alternatecase!(shift = 0)
    replace(alternatecase(shift))
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ctf-party-3.0.0 lib/ctf_party/case.rb
ctf-party-2.3.0 lib/ctf_party/case.rb
ctf-party-2.2.0 lib/ctf_party/case.rb
ctf-party-2.1.0 lib/ctf_party/case.rb
ctf-party-2.0.0 lib/ctf_party/case.rb
ctf-party-1.5.0 lib/ctf_party/case.rb
ctf-party-1.4.1 lib/ctf_party/case.rb
ctf-party-1.4.0 lib/ctf_party/case.rb
ctf-party-1.3.5 lib/ctf_party/case.rb
ctf-party-1.3.4 lib/ctf_party/case.rb
ctf-party-1.3.3 lib/ctf_party/case.rb
ctf-party-1.3.2 lib/ctf_party/case.rb
ctf-party-1.3.1 lib/ctf_party/case.rb
ctf-party-1.3.0 lib/ctf_party/case.rb