openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

index.html (5749B)


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