diff options
Diffstat (limited to 'imports/codemirror/mode/ruby/index.html')
-rwxr-xr-x | imports/codemirror/mode/ruby/index.html | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/imports/codemirror/mode/ruby/index.html b/imports/codemirror/mode/ruby/index.html new file mode 100755 index 00000000..6d33db19 --- /dev/null +++ b/imports/codemirror/mode/ruby/index.html | |||
@@ -0,0 +1,171 @@ | |||
1 | <!doctype html> | ||
2 | <html> | ||
3 | <head> | ||
4 | <title>CodeMirror: Ruby mode</title> | ||
5 | <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
6 | <script src="../../lib/codemirror.js"></script> | ||
7 | <script src="ruby.js"></script> | ||
8 | <style> | ||
9 | .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;} | ||
10 | .cm-s-default span.cm-arrow { color: red; } | ||
11 | </style> | ||
12 | <link rel="stylesheet" href="../../doc/docs.css"> | ||
13 | </head> | ||
14 | <body> | ||
15 | <h1>CodeMirror: Ruby mode</h1> | ||
16 | <form><textarea id="code" name="code"> | ||
17 | # Code from http://sandbox.mc.edu/~bennet/ruby/code/poly_rb.html | ||
18 | # | ||
19 | # This program evaluates polynomials. It first asks for the coefficients | ||
20 | # of a polynomial, which must be entered on one line, highest-order first. | ||
21 | # It then requests values of x and will compute the value of the poly for | ||
22 | # each x. It will repeatly ask for x values, unless you the user enters | ||
23 | # a blank line. It that case, it will ask for another polynomial. If the | ||
24 | # user types quit for either input, the program immediately exits. | ||
25 | # | ||
26 | |||
27 | # | ||
28 | # Function to evaluate a polynomial at x. The polynomial is given | ||
29 | # as a list of coefficients, from the greatest to the least. | ||
30 | def polyval(x, coef) | ||
31 | sum = 0 | ||
32 | coef = coef.clone # Don't want to destroy the original | ||
33 | while true | ||
34 | sum += coef.shift # Add and remove the next coef | ||
35 | break if coef.empty? # If no more, done entirely. | ||
36 | sum *= x # This happens the right number of times. | ||
37 | end | ||
38 | return sum | ||
39 | end | ||
40 | |||
41 | # | ||
42 | # Function to read a line containing a list of integers and return | ||
43 | # them as an array of integers. If the string conversion fails, it | ||
44 | # throws TypeError. If the input line is the word 'quit', then it | ||
45 | # converts it to an end-of-file exception | ||
46 | def readints(prompt) | ||
47 | # Read a line | ||
48 | print prompt | ||
49 | line = readline.chomp | ||
50 | raise EOFError.new if line == 'quit' # You can also use a real EOF. | ||
51 | |||
52 | # Go through each item on the line, converting each one and adding it | ||
53 | # to retval. | ||
54 | retval = [ ] | ||
55 | for str in line.split(/\s+/) | ||
56 | if str =~ /^\-?\d+$/ | ||
57 | retval.push(str.to_i) | ||
58 | else | ||
59 | raise TypeError.new | ||
60 | end | ||
61 | end | ||
62 | |||
63 | return retval | ||
64 | end | ||
65 | |||
66 | # | ||
67 | # Take a coeff and an exponent and return the string representation, ignoring | ||
68 | # the sign of the coefficient. | ||
69 | def term_to_str(coef, exp) | ||
70 | ret = "" | ||
71 | |||
72 | # Show coeff, unless it's 1 or at the right | ||
73 | coef = coef.abs | ||
74 | ret = coef.to_s unless coef == 1 && exp > 0 | ||
75 | ret += "x" if exp > 0 # x if exponent not 0 | ||
76 | ret += "^" + exp.to_s if exp > 1 # ^exponent, if > 1. | ||
77 | |||
78 | return ret | ||
79 | end | ||
80 | |||
81 | # | ||
82 | # Create a string of the polynomial in sort-of-readable form. | ||
83 | def polystr(p) | ||
84 | # Get the exponent of first coefficient, plus 1. | ||
85 | exp = p.length | ||
86 | |||
87 | # Assign exponents to each term, making pairs of coeff and exponent, | ||
88 | # Then get rid of the zero terms. | ||
89 | p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 } | ||
90 | |||
91 | # If there's nothing left, it's a zero | ||
92 | return "0" if p.empty? | ||
93 | |||
94 | # *** Now p is a non-empty list of [ coef, exponent ] pairs. *** | ||
95 | |||
96 | # Convert the first term, preceded by a "-" if it's negative. | ||
97 | result = (if p[0][0] < 0 then "-" else "" end) + term_to_str(*p[0]) | ||
98 | |||
99 | # Convert the rest of the terms, in each case adding the appropriate | ||
100 | # + or - separating them. | ||
101 | for term in p[1...p.length] | ||
102 | # Add the separator then the rep. of the term. | ||
103 | result += (if term[0] < 0 then " - " else " + " end) + | ||
104 | term_to_str(*term) | ||
105 | end | ||
106 | |||
107 | return result | ||
108 | end | ||
109 | |||
110 | # | ||
111 | # Run until some kind of endfile. | ||
112 | begin | ||
113 | # Repeat until an exception or quit gets us out. | ||
114 | while true | ||
115 | # Read a poly until it works. An EOF will except out of the | ||
116 | # program. | ||
117 | print "\n" | ||
118 | begin | ||
119 | poly = readints("Enter a polynomial coefficients: ") | ||
120 | rescue TypeError | ||
121 | print "Try again.\n" | ||
122 | retry | ||
123 | end | ||
124 | break if poly.empty? | ||
125 | |||
126 | # Read and evaluate x values until the user types a blank line. | ||
127 | # Again, an EOF will except out of the pgm. | ||
128 | while true | ||
129 | # Request an integer. | ||
130 | print "Enter x value or blank line: " | ||
131 | x = readline.chomp | ||
132 | break if x == '' | ||
133 | raise EOFError.new if x == 'quit' | ||
134 | |||
135 | # If it looks bad, let's try again. | ||
136 | if x !~ /^\-?\d+$/ | ||
137 | print "That doesn't look like an integer. Please try again.\n" | ||
138 | next | ||
139 | end | ||
140 | |||
141 | # Convert to an integer and print the result. | ||
142 | x = x.to_i | ||
143 | print "p(x) = ", polystr(poly), "\n" | ||
144 | print "p(", x, ") = ", polyval(x, poly), "\n" | ||
145 | end | ||
146 | end | ||
147 | rescue EOFError | ||
148 | print "\n=== EOF ===\n" | ||
149 | rescue Interrupt, SignalException | ||
150 | print "\n=== Interrupted ===\n" | ||
151 | else | ||
152 | print "--- Bye ---\n" | ||
153 | end | ||
154 | </textarea></form> | ||
155 | <script> | ||
156 | var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | ||
157 | mode: "text/x-ruby", | ||
158 | tabMode: "indent", | ||
159 | matchBrackets: true, | ||
160 | indentUnit: 4 | ||
161 | }); | ||
162 | </script> | ||
163 | |||
164 | <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p> | ||
165 | |||
166 | <p>Development of the CodeMirror Ruby mode was kindly sponsored | ||
167 | by <a href="http://ubalo.com/">Ubalo</a>, who hold | ||
168 | the <a href="LICENSE">license</a>.</p> | ||
169 | |||
170 | </body> | ||
171 | </html> | ||