5 <title>CodeMirror: Ruby mode</title>
6 <link rel="stylesheet" href="../../lib/codemirror.css">
7 <script src="../../lib/codemirror.js"></script>
8 <script src="../../addon/edit/matchbrackets.js"></script>
9 <script src="ruby.js"></script>
11 .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
12 .cm-s-default span.cm-arrow { color: red; }
14 <link rel="stylesheet" href="../../doc/docs.css">
17 <h1>CodeMirror: Ruby mode</h1>
18 <form><textarea id="code" name="code">
19 # Code from http://sandbox.mc.edu/~bennet/ruby/code/poly_rb.html
21 # This program evaluates polynomials. It first asks for the coefficients
22 # of a polynomial, which must be entered on one line, highest-order first.
23 # It then requests values of x and will compute the value of the poly for
24 # each x. It will repeatly ask for x values, unless you the user enters
25 # a blank line. It that case, it will ask for another polynomial. If the
26 # user types quit for either input, the program immediately exits.
30 # Function to evaluate a polynomial at x. The polynomial is given
31 # as a list of coefficients, from the greatest to the least.
34 coef = coef.clone # Don't want to destroy the original
36 sum += coef.shift # Add and remove the next coef
37 break if coef.empty? # If no more, done entirely.
38 sum *= x # This happens the right number of times.
44 # Function to read a line containing a list of integers and return
45 # them as an array of integers. If the string conversion fails, it
46 # throws TypeError. If the input line is the word 'quit', then it
47 # converts it to an end-of-file exception
52 raise EOFError.new if line == 'quit' # You can also use a real EOF.
54 # Go through each item on the line, converting each one and adding it
57 for str in line.split(/\s+/)
69 # Take a coeff and an exponent and return the string representation, ignoring
70 # the sign of the coefficient.
71 def term_to_str(coef, exp)
74 # Show coeff, unless it's 1 or at the right
76 ret = coef.to_s unless coef == 1 && exp > 0
77 ret += "x" if exp > 0 # x if exponent not 0
78 ret += "^" + exp.to_s if exp > 1 # ^exponent, if > 1.
84 # Create a string of the polynomial in sort-of-readable form.
86 # Get the exponent of first coefficient, plus 1.
89 # Assign exponents to each term, making pairs of coeff and exponent,
90 # Then get rid of the zero terms.
91 p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 }
93 # If there's nothing left, it's a zero
94 return "0" if p.empty?
96 # *** Now p is a non-empty list of [ coef, exponent ] pairs. ***
98 # Convert the first term, preceded by a "-" if it's negative.
99 result = (if p[0][0] < 0 then "-" else "" end) + term_to_str(*p[0])
101 # Convert the rest of the terms, in each case adding the appropriate
102 # + or - separating them.
103 for term in p[1...p.length]
104 # Add the separator then the rep. of the term.
105 result += (if term[0] < 0 then " - " else " + " end) +
113 # Run until some kind of endfile.
115 # Repeat until an exception or quit gets us out.
117 # Read a poly until it works. An EOF will except out of the
121 poly = readints("Enter a polynomial coefficients: ")
128 # Read and evaluate x values until the user types a blank line.
129 # Again, an EOF will except out of the pgm.
131 # Request an integer.
132 print "Enter x value or blank line: "
135 raise EOFError.new if x == 'quit'
137 # If it looks bad, let's try again.
139 print "That doesn't look like an integer. Please try again.\n"
143 # Convert to an integer and print the result.
145 print "p(x) = ", polystr(poly), "\n"
146 print "p(", x, ") = ", polyval(x, poly), "\n"
150 print "\n=== EOF ===\n"
151 rescue Interrupt, SignalException
152 print "\n=== Interrupted ===\n"
154 print "--- Bye ---\n"
158 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
166 <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p>
168 <p>Development of the CodeMirror Ruby mode was kindly sponsored
169 by <a href="http://ubalo.com/">Ubalo</a>, who hold
170 the <a href="LICENSE">license</a>.</p>