Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 39 lines (32 sloc) 0.79 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
require 'coderay'

SAMPLE = "puts 17 + 4\n"
puts 'Encoders Demo: ' + SAMPLE
scanner = CodeRay::Scanners[:ruby].new SAMPLE
encoder = CodeRay::Encoders[:statistic].new

tokens = scanner.tokenize
stats = encoder.encode_tokens tokens

puts
puts 'Statistic:'
puts stats

# alternative 1
tokens = CodeRay.scan SAMPLE, :ruby
encoder = CodeRay.encoder(:json)
textual = encoder.encode_tokens tokens
puts
puts 'Original text:'
puts textual

# alternative 2
yaml = CodeRay.encoder(:yaml).encode SAMPLE, :ruby
puts
puts 'YAML:'
puts yaml

# alternative 3
BIGSAMPLE = SAMPLE * 100
dump = CodeRay.scan(BIGSAMPLE, :ruby).dump
puts
puts 'Dump:'
p dump
puts 'compressed: %d byte < %d byte' % [dump.size, BIGSAMPLE.size]

puts
puts 'Undump:'
puts dump.undump.statistic
Something went wrong with that request. Please try again.