You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.6 KiB

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