2
0

index.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  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>
  10. <style>
  11. .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
  12. .cm-s-default span.cm-arrow { color: red; }
  13. </style>
  14. <link rel="stylesheet" href="../../doc/docs.css">
  15. </head>
  16. <body>
  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
  20. #
  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.
  27. #
  28. #
  29. # Function to evaluate a polynomial at x. The polynomial is given
  30. # as a list of coefficients, from the greatest to the least.
  31. def polyval(x, coef)
  32. sum = 0
  33. coef = coef.clone # Don't want to destroy the original
  34. while true
  35. sum += coef.shift # Add and remove the next coef
  36. break if coef.empty? # If no more, done entirely.
  37. sum *= x # This happens the right number of times.
  38. end
  39. return sum
  40. end
  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. # Go through each item on the line, converting each one and adding it
  52. # to retval.
  53. retval = [ ]
  54. for str in line.split(/\s+/)
  55. if str =~ /^\-?\d+$/
  56. retval.push(str.to_i)
  57. else
  58. raise TypeError.new
  59. end
  60. end
  61. return retval
  62. end
  63. #
  64. # Take a coeff and an exponent and return the string representation, ignoring
  65. # the sign of the coefficient.
  66. def term_to_str(coef, exp)
  67. ret = ""
  68. # Show coeff, unless it's 1 or at the right
  69. coef = coef.abs
  70. ret = coef.to_s unless coef == 1 && exp > 0
  71. ret += "x" if exp > 0 # x if exponent not 0
  72. ret += "^" + exp.to_s if exp > 1 # ^exponent, if > 1.
  73. return ret
  74. end
  75. #
  76. # Create a string of the polynomial in sort-of-readable form.
  77. def polystr(p)
  78. # Get the exponent of first coefficient, plus 1.
  79. exp = p.length
  80. # Assign exponents to each term, making pairs of coeff and exponent,
  81. # Then get rid of the zero terms.
  82. p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 }
  83. # If there's nothing left, it's a zero
  84. return "0" if p.empty?
  85. # *** Now p is a non-empty list of [ coef, exponent ] pairs. ***
  86. # Convert the first term, preceded by a "-" if it's negative.
  87. result = (if p[0][0] < 0 then "-" else "" end) + term_to_str(*p[0])
  88. # Convert the rest of the terms, in each case adding the appropriate
  89. # + or - separating them.
  90. for term in p[1...p.length]
  91. # Add the separator then the rep. of the term.
  92. result += (if term[0] < 0 then " - " else " + " end) +
  93. term_to_str(*term)
  94. end
  95. return result
  96. end
  97. #
  98. # Run until some kind of endfile.
  99. begin
  100. # Repeat until an exception or quit gets us out.
  101. while true
  102. # Read a poly until it works. An EOF will except out of the
  103. # program.
  104. print "\n"
  105. begin
  106. poly = readints("Enter a polynomial coefficients: ")
  107. rescue TypeError
  108. print "Try again.\n"
  109. retry
  110. end
  111. break if poly.empty?
  112. # Read and evaluate x values until the user types a blank line.
  113. # Again, an EOF will except out of the pgm.
  114. while true
  115. # Request an integer.
  116. print "Enter x value or blank line: "
  117. x = readline.chomp
  118. break if x == ''
  119. raise EOFError.new if x == 'quit'
  120. # If it looks bad, let's try again.
  121. if x !~ /^\-?\d+$/
  122. print "That doesn't look like an integer. Please try again.\n"
  123. next
  124. end
  125. # Convert to an integer and print the result.
  126. x = x.to_i
  127. print "p(x) = ", polystr(poly), "\n"
  128. print "p(", x, ") = ", polyval(x, poly), "\n"
  129. end
  130. end
  131. rescue EOFError
  132. print "\n=== EOF ===\n"
  133. rescue Interrupt, SignalException
  134. print "\n=== Interrupted ===\n"
  135. else
  136. print "--- Bye ---\n"
  137. end
  138. </textarea></form>
  139. <script>
  140. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  141. mode: "text/x-ruby",
  142. tabMode: "indent",
  143. matchBrackets: true,
  144. indentUnit: 4
  145. });
  146. </script>
  147. <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p>
  148. <p>Development of the CodeMirror Ruby mode was kindly sponsored
  149. by <a href="http://ubalo.com/">Ubalo</a>, who hold
  150. the <a href="LICENSE">license</a>.</p>
  151. </body>
  152. </html>