Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/coderay/helpers/gzip_simple.rb,
lib/coderay/scanner.rb

Overview

String extensions to use the GZip module.

The methods gzip and gunzip provide an even more simple interface to the ZLib:

# create a big string
x = 'a' * 1000

# zip it
x_gz = x.gzip

# test the result
puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
#-> Zipped 1000 bytes to 19 bytes.

# unzipping works
p x_gz.gunzip == x  #-> true

Instance Method Summary collapse

Instance Method Details

#gunzipObject

Returns the string, unzipped. See GZip.gunzip



70
71
72
# File 'lib/coderay/helpers/gzip_simple.rb', line 70

def gunzip
  GZip.gunzip self
end

#gunzip!Object

Replaces the string with its unzipped value. See GZip.gunzip



75
76
77
# File 'lib/coderay/helpers/gzip_simple.rb', line 75

def gunzip!
  replace gunzip
end

#gzip(level = GZip::DEFAULT_GZIP_LEVEL) ⇒ Object

Returns the string, zipped. level is the gzip compression level, see GZip.gzip.



81
82
83
# File 'lib/coderay/helpers/gzip_simple.rb', line 81

def gzip level = GZip::DEFAULT_GZIP_LEVEL
  GZip.gzip self, level
end

#gzip!(*args) ⇒ Object

Replaces the string with its zipped value. See GZip.gzip.



86
87
88
# File 'lib/coderay/helpers/gzip_simple.rb', line 86

def gzip!(*args)
  replace gzip(*args)
end

#to_unixObject

I love this hack. It seems to silence all dos/unix/mac newline problems.



277
278
279
280
281
282
283
# File 'lib/coderay/scanner.rb', line 277

def to_unix
  if index ?\r
    gsub(/\r\n?/, "\n")
  else
    self
  end
end