Module: Obfuscator

Defined in:
lib/number_obfuscator.rb,
lib/number_obfuscator/tex.rb,
lib/number_obfuscator/png.rb,
lib/number_obfuscator/number.rb,
lib/number_obfuscator/division.rb,
lib/number_obfuscator/summation.rb,
lib/number_obfuscator/expression.rb,
lib/number_obfuscator/square_root.rb,
lib/number_obfuscator/subtraction.rb,
lib/number_obfuscator/multiplication.rb,
lib/number_obfuscator/binary_expression.rb

Overview

The namespace module which all number_obfuscator stuff goes into.

Helper methods Obfuscator.makePNG and Obfuscator.makeTeX are in separate files.

Defined Under Namespace

Classes: BinaryExpression, Division, Expression, Multiplication, Number, SquareRoot, Subtraction, Summation

Class Method Summary collapse

Class Method Details

.generate(n, depth = 4) ⇒ Expression

Returns a new, randomly generated expression tree for a given number.

Parameters:

  • n (Integer)

    The number to be obfuscated.

  • depth (Integer) (defaults to: 4)

    Desired depth (complexity) of the expression.

Returns:


24
25
26
# File 'lib/number_obfuscator.rb', line 24

def Obfuscator.generate(n, depth = 4)
  Expression.makeExpression(n, depth)
end

.makePNG(e, filename) ⇒ Object

Use this helper method to create a .png file for a given expression.


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/number_obfuscator/png.rb', line 4

def Obfuscator.makePNG(e, filename)
  tempfile = Tempfile.new('obfuscator').path

  File.open("#{tempfile}.tex", 'w') { |f|
    f.puts makeTeX(e.to_tex)
  }

  texcall = "texi2dvi --build=clean --build-dir=#{tempfile}.t2d #{tempfile}.tex -o #{tempfile}.dvi"
  pngcall = "dvipng -D 300 -T tight -o #{filename} #{tempfile}.dvi"

  # TODO: redirecting to /dev/null probably breaks Windows..
  if ! system(texcall, :out => '/dev/null' )
    raise "texi2dvi failed. Is it (and a TeX installation such as texlive) installed?"
  end

  if ! system(pngcall, :out => '/dev/null')
    raise "dvipng failed. Is it installed?"
  end

  system("rm -rf #{tempfile}*")
end

.makeTeX(s) ⇒ Object

Use this helper method to create a full TeX document around a formula.


5
6
7
8
9
10
11
12
13
# File 'lib/number_obfuscator/tex.rb', line 5

def Obfuscator.makeTeX(s)
  tex = (Expression === s) ? s.to_tex : s.to_s
  "\\documentclass{minimal}\n" + 
  "\\usepackage[paperwidth=100cm,paperheight=100cm]{geometry}" +
  "\\pagestyle{empty}\n"       + 
  "\\begin{document}\n"        + 
  "$$ #{tex} $$\n"             + 
  "\\end{document}\n"
end