Sha256: 764dec35ddd74f297ae1e8697d8134ab3a35a4acf91fab043fe1ba22006dc426

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

require 'string/bracket'

class String

  #def self.bra2ket
  #  @bra2ket ||= { '['=>']', '('=>')', '{'=>'}', '<'=>'>' }
  #end

  # Return a new string embraced by given brakets.
  # If only one bracket char is given it will be placed
  # on either side.
  #
  #   "{unwrap me}".debracket('{')        #=> "unwrap me"
  #   "--unwrap me!".debracket('--','!')  #=> "unwrap me!"
  #
  def unbracket(bra=nil, ket=nil)
    if bra
      ket = Sring.bra2ket[bra] unless ket
      ket = ket ? ket : bra
      s = self.dup
      s.gsub!(%r[^#{Regexp.escape(bra)}], '')
      s.gsub!(%r[#{Regexp.escape(ket)}$], '')
      return s
    else
      if m = String.bra2ket[ self[0,1] ]
        return self.slice(1...-1) if self[-1,1]  == m
      end
    end
    return self.dup  # if nothing else
  end

  # Inplace version of #debraket.
  def unbracket!(bra=nil, ket=nil)
    self.replace( unbracket(bra, ket) )
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
# TODO

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/string/unbracket.rb
facets-1.0.0 lib/facet/string/unbracket.rb
facets-1.0.3 packages/core/lib/facet/string/unbracket.rb