Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
5 lars 1
<!doctype html>
2
<title>CodeMirror: Ruby mode</title>
3
<meta charset="utf-8" />
4
<link rel=stylesheet href="../../doc/docs.css">
5
<link rel="stylesheet" href="../../lib/codemirror.css">
6
<script src="../../lib/codemirror.js"></script>
7
<script src="../../addon/edit/matchbrackets.js"></script>
8
<script src="ruby.js"></script>
9
<style>
10
    .CodeMirror {
11
        border-top: 1px solid black;
12
        border-bottom: 1px solid black;
13
    }
14
 
15
    .cm-s-default span.cm-arrow {
16
        color: red;
17
    }
18
</style>
19
<div id=nav>
20
    <a href="http://codemirror.net">
21
        <h1>CodeMirror</h1>
22
        <img id=logo src="../../doc/logo.png">
23
    </a>
24
    <ul>
25
        <li>
26
            <a href="../../index.html">Home</a>
27
            <li>
28
                <a href="../../doc/manual.html">Manual</a>
29
                <li>
30
                    <a href="https://github.com/codemirror/codemirror">Code</a>
31
    </ul>
32
    <ul>
33
        <li>
34
            <a href="../index.html">Language modes</a>
35
            <li>
36
                <a class=active href="#">Ruby</a>
37
    </ul>
38
</div>
39
<article>
40
    <h2>Ruby mode</h2>
41
    <form>
42
        <textarea id="code" name="code"> # Code from http://sandbox.mc.edu/~bennet/ruby/code/poly_rb.html # # This program evaluates polynomials. It first asks for the coefficients # of a polynomial, which must be entered on one line, highest-order first. # It then requests values of
43
            x and will compute the value of the poly for # each x. It will repeatly ask for x values, unless you the user enters # a blank line. It that case, it will ask for another polynomial. If the # user types quit for either input, the program immediately
44
            exits. # # # Function to evaluate a polynomial at x. The polynomial is given # as a list of coefficients, from the greatest to the least. def polyval(x, coef) sum = 0 coef = coef.clone # Don't want to destroy the original while true sum +=
45
            coef.shift # Add and remove the next coef break if coef.empty? # If no more, done entirely. sum *= x # This happens the right number of times. end return sum end # # Function to read a line containing a list of integers and return # them as
46
            an array of integers. If the string conversion fails, it # throws TypeError. If the input line is the word 'quit', then it # converts it to an end-of-file exception def readints(prompt) # Read a line print prompt line = readline.chomp raise
47
            EOFError.new if line == 'quit' # You can also use a real EOF. # Go through each item on the line, converting each one and adding it # to retval. retval = [ ] for str in line.split(/\s+/) if str =~ /^\-?\d+$/ retval.push(str.to_i) else raise
48
            TypeError.new end end return retval end # # Take a coeff and an exponent and return the string representation, ignoring # the sign of the coefficient. def term_to_str(coef, exp) ret = "" # Show coeff, unless it's 1 or at the right coef = coef.abs
49
            ret = coef.to_s unless coef == 1 && exp > 0 ret += "x" if exp > 0 # x if exponent not 0 ret += "^" + exp.to_s if exp > 1 # ^exponent, if > 1. return ret end # # Create a string of the polynomial in sort-of-readable form. def polystr(p) # Get
50
            the exponent of first coefficient, plus 1. exp = p.length # Assign exponents to each term, making pairs of coeff and exponent, # Then get rid of the zero terms. p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 } # If there's
51
            nothing left, it's a zero return "0" if p.empty? # *** Now p is a non-empty list of [ coef, exponent ] pairs. *** # Convert the first term, preceded by a "-" if it's negative. result = (if p[0][0]
52
            < 0 then "-" else "" end) + term_to_str(*p[0])
53
                # Convert the rest of the terms, in each case adding the appropriate # + or - separating them. for term in p[1...p.length] # Add the separator then the rep. of the term. result +=( if term[0] < 0 then " - " else " + " end) + term_to_str(*term)
54
                end return result end # # Run until some kind of endfile. begin # Repeat until an exception or quit gets us out. while true # Read a poly until it works. An EOF will except out of the # program. print "\n" begin poly=r eadints(
55
                "Enter a polynomial coefficients: ") rescue TypeError print "Try again.\n" retry end break if poly.empty? # Read and evaluate x values until the user types a blank line. # Again, an EOF will except out of the pgm. while true # Request an integer. print
56
                "Enter x value or blank line: " x=r eadline.chomp break if x=='' raise EOFError.new if x=='quit' # If it looks bad, let 's try again.
57
            if x !~ /^\-?\d+$/
58
                print "That doesn't look like an integer. Please try again.\n "
59
                next
60
            end
61
 
62
            # Convert to an integer and print the result.
63
            x = x.to_i
64
            print "p(x)=", polystr(poly), " \n "
65
            print "p( ", x, ")=", polyval(x, poly), " \n "
66
        end
67
    end
68
rescue EOFError
69
    print "\n===E OF===\n "
70
rescue Interrupt, SignalException
71
    print "\n===I nterrupted===\n "
72
else
73
    print "--- Bye ---\n "
74
end
75
</textarea></form>
76
    <script>
77
      var editor = CodeMirror.fromTextArea(document.getElementById("code "), {
78
        mode: "text/x-ruby ",
79
        matchBrackets: true,
80
        indentUnit: 4
81
      });
82
    </script>
83
 
84
    <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p>
85
 
86
    <p>Development of the CodeMirror Ruby mode was kindly sponsored
87
    by <a href="http://ubalo.com/ ">Ubalo</a>.</p>
88
 
89
  </article>