gitlab-pygments.rb 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (399) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +2 -0
  3. data/README.md +91 -0
  4. data/Rakefile +78 -0
  5. data/bench.rb +22 -0
  6. data/cache-lexers.rb +8 -0
  7. data/lexers +0 -0
  8. data/lib/pygments/lexer.rb +148 -0
  9. data/lib/pygments/mentos.py +344 -0
  10. data/lib/pygments/popen.rb +389 -0
  11. data/lib/pygments/version.rb +3 -0
  12. data/lib/pygments.rb +8 -0
  13. data/pygments.rb.gemspec +24 -0
  14. data/test/test_data.c +2581 -0
  15. data/test/test_data.py +514 -0
  16. data/test/test_data_generated +2582 -0
  17. data/test/test_pygments.rb +276 -0
  18. data/vendor/custom_formatters/gitlab.py +171 -0
  19. data/vendor/custom_lexers/github.py +362 -0
  20. data/vendor/pygments-main/AUTHORS +115 -0
  21. data/vendor/pygments-main/CHANGES +762 -0
  22. data/vendor/pygments-main/LICENSE +25 -0
  23. data/vendor/pygments-main/MANIFEST.in +6 -0
  24. data/vendor/pygments-main/Makefile +59 -0
  25. data/vendor/pygments-main/REVISION +1 -0
  26. data/vendor/pygments-main/TODO +15 -0
  27. data/vendor/pygments-main/docs/generate.py +472 -0
  28. data/vendor/pygments-main/docs/pygmentize.1 +94 -0
  29. data/vendor/pygments-main/docs/src/api.txt +270 -0
  30. data/vendor/pygments-main/docs/src/authors.txt +5 -0
  31. data/vendor/pygments-main/docs/src/changelog.txt +5 -0
  32. data/vendor/pygments-main/docs/src/cmdline.txt +147 -0
  33. data/vendor/pygments-main/docs/src/filterdevelopment.txt +70 -0
  34. data/vendor/pygments-main/docs/src/filters.txt +42 -0
  35. data/vendor/pygments-main/docs/src/formatterdevelopment.txt +169 -0
  36. data/vendor/pygments-main/docs/src/formatters.txt +48 -0
  37. data/vendor/pygments-main/docs/src/index.txt +69 -0
  38. data/vendor/pygments-main/docs/src/installation.txt +71 -0
  39. data/vendor/pygments-main/docs/src/integrate.txt +43 -0
  40. data/vendor/pygments-main/docs/src/lexerdevelopment.txt +551 -0
  41. data/vendor/pygments-main/docs/src/lexers.txt +67 -0
  42. data/vendor/pygments-main/docs/src/moinmoin.txt +39 -0
  43. data/vendor/pygments-main/docs/src/plugins.txt +93 -0
  44. data/vendor/pygments-main/docs/src/quickstart.txt +202 -0
  45. data/vendor/pygments-main/docs/src/rstdirective.txt +22 -0
  46. data/vendor/pygments-main/docs/src/styles.txt +143 -0
  47. data/vendor/pygments-main/docs/src/tokens.txt +349 -0
  48. data/vendor/pygments-main/docs/src/unicode.txt +49 -0
  49. data/vendor/pygments-main/external/markdown-processor.py +67 -0
  50. data/vendor/pygments-main/external/moin-parser.py +112 -0
  51. data/vendor/pygments-main/external/pygments.bashcomp +38 -0
  52. data/vendor/pygments-main/external/rst-directive-old.py +77 -0
  53. data/vendor/pygments-main/external/rst-directive.py +83 -0
  54. data/vendor/pygments-main/ez_setup.py +276 -0
  55. data/vendor/pygments-main/pygmentize +7 -0
  56. data/vendor/pygments-main/pygments/__init__.py +91 -0
  57. data/vendor/pygments-main/pygments/cmdline.py +433 -0
  58. data/vendor/pygments-main/pygments/console.py +74 -0
  59. data/vendor/pygments-main/pygments/filter.py +74 -0
  60. data/vendor/pygments-main/pygments/filters/__init__.py +357 -0
  61. data/vendor/pygments-main/pygments/formatter.py +92 -0
  62. data/vendor/pygments-main/pygments/formatters/__init__.py +68 -0
  63. data/vendor/pygments-main/pygments/formatters/_mapping.py +94 -0
  64. data/vendor/pygments-main/pygments/formatters/bbcode.py +109 -0
  65. data/vendor/pygments-main/pygments/formatters/gitlab.py +171 -0
  66. data/vendor/pygments-main/pygments/formatters/html.py +750 -0
  67. data/vendor/pygments-main/pygments/formatters/img.py +553 -0
  68. data/vendor/pygments-main/pygments/formatters/latex.py +378 -0
  69. data/vendor/pygments-main/pygments/formatters/other.py +117 -0
  70. data/vendor/pygments-main/pygments/formatters/rtf.py +136 -0
  71. data/vendor/pygments-main/pygments/formatters/svg.py +154 -0
  72. data/vendor/pygments-main/pygments/formatters/terminal.py +112 -0
  73. data/vendor/pygments-main/pygments/formatters/terminal256.py +222 -0
  74. data/vendor/pygments-main/pygments/lexer.py +697 -0
  75. data/vendor/pygments-main/pygments/lexers/__init__.py +229 -0
  76. data/vendor/pygments-main/pygments/lexers/_asybuiltins.py +1645 -0
  77. data/vendor/pygments-main/pygments/lexers/_clbuiltins.py +232 -0
  78. data/vendor/pygments-main/pygments/lexers/_luabuiltins.py +249 -0
  79. data/vendor/pygments-main/pygments/lexers/_mapping.py +298 -0
  80. data/vendor/pygments-main/pygments/lexers/_phpbuiltins.py +3787 -0
  81. data/vendor/pygments-main/pygments/lexers/_postgres_builtins.py +232 -0
  82. data/vendor/pygments-main/pygments/lexers/_scilab_builtins.py +29 -0
  83. data/vendor/pygments-main/pygments/lexers/_vimbuiltins.py +3 -0
  84. data/vendor/pygments-main/pygments/lexers/agile.py +1803 -0
  85. data/vendor/pygments-main/pygments/lexers/asm.py +360 -0
  86. data/vendor/pygments-main/pygments/lexers/compiled.py +2891 -0
  87. data/vendor/pygments-main/pygments/lexers/dotnet.py +636 -0
  88. data/vendor/pygments-main/pygments/lexers/functional.py +1832 -0
  89. data/vendor/pygments-main/pygments/lexers/github.py +362 -0
  90. data/vendor/pygments-main/pygments/lexers/hdl.py +356 -0
  91. data/vendor/pygments-main/pygments/lexers/jvm.py +847 -0
  92. data/vendor/pygments-main/pygments/lexers/math.py +1072 -0
  93. data/vendor/pygments-main/pygments/lexers/other.py +3339 -0
  94. data/vendor/pygments-main/pygments/lexers/parsers.py +695 -0
  95. data/vendor/pygments-main/pygments/lexers/shell.py +361 -0
  96. data/vendor/pygments-main/pygments/lexers/special.py +100 -0
  97. data/vendor/pygments-main/pygments/lexers/sql.py +559 -0
  98. data/vendor/pygments-main/pygments/lexers/templates.py +1631 -0
  99. data/vendor/pygments-main/pygments/lexers/text.py +1753 -0
  100. data/vendor/pygments-main/pygments/lexers/web.py +2864 -0
  101. data/vendor/pygments-main/pygments/plugin.py +74 -0
  102. data/vendor/pygments-main/pygments/scanner.py +104 -0
  103. data/vendor/pygments-main/pygments/style.py +117 -0
  104. data/vendor/pygments-main/pygments/styles/__init__.py +70 -0
  105. data/vendor/pygments-main/pygments/styles/autumn.py +65 -0
  106. data/vendor/pygments-main/pygments/styles/borland.py +51 -0
  107. data/vendor/pygments-main/pygments/styles/bw.py +49 -0
  108. data/vendor/pygments-main/pygments/styles/colorful.py +81 -0
  109. data/vendor/pygments-main/pygments/styles/default.py +73 -0
  110. data/vendor/pygments-main/pygments/styles/emacs.py +72 -0
  111. data/vendor/pygments-main/pygments/styles/friendly.py +72 -0
  112. data/vendor/pygments-main/pygments/styles/fruity.py +42 -0
  113. data/vendor/pygments-main/pygments/styles/manni.py +75 -0
  114. data/vendor/pygments-main/pygments/styles/monokai.py +106 -0
  115. data/vendor/pygments-main/pygments/styles/murphy.py +80 -0
  116. data/vendor/pygments-main/pygments/styles/native.py +65 -0
  117. data/vendor/pygments-main/pygments/styles/pastie.py +75 -0
  118. data/vendor/pygments-main/pygments/styles/perldoc.py +69 -0
  119. data/vendor/pygments-main/pygments/styles/rrt.py +33 -0
  120. data/vendor/pygments-main/pygments/styles/tango.py +141 -0
  121. data/vendor/pygments-main/pygments/styles/trac.py +63 -0
  122. data/vendor/pygments-main/pygments/styles/vim.py +63 -0
  123. data/vendor/pygments-main/pygments/styles/vs.py +38 -0
  124. data/vendor/pygments-main/pygments/token.py +195 -0
  125. data/vendor/pygments-main/pygments/unistring.py +130 -0
  126. data/vendor/pygments-main/pygments/util.py +232 -0
  127. data/vendor/pygments-main/scripts/check_sources.py +242 -0
  128. data/vendor/pygments-main/scripts/detect_missing_analyse_text.py +30 -0
  129. data/vendor/pygments-main/scripts/epydoc.css +280 -0
  130. data/vendor/pygments-main/scripts/find_codetags.py +205 -0
  131. data/vendor/pygments-main/scripts/find_error.py +171 -0
  132. data/vendor/pygments-main/scripts/get_vimkw.py +43 -0
  133. data/vendor/pygments-main/scripts/pylintrc +301 -0
  134. data/vendor/pygments-main/scripts/reindent.py +291 -0
  135. data/vendor/pygments-main/scripts/vim2pygments.py +933 -0
  136. data/vendor/pygments-main/setup.cfg +6 -0
  137. data/vendor/pygments-main/setup.py +88 -0
  138. data/vendor/pygments-main/tests/dtds/HTML4-f.dtd +37 -0
  139. data/vendor/pygments-main/tests/dtds/HTML4-s.dtd +869 -0
  140. data/vendor/pygments-main/tests/dtds/HTML4.dcl +88 -0
  141. data/vendor/pygments-main/tests/dtds/HTML4.dtd +1092 -0
  142. data/vendor/pygments-main/tests/dtds/HTML4.soc +9 -0
  143. data/vendor/pygments-main/tests/dtds/HTMLlat1.ent +195 -0
  144. data/vendor/pygments-main/tests/dtds/HTMLspec.ent +77 -0
  145. data/vendor/pygments-main/tests/dtds/HTMLsym.ent +241 -0
  146. data/vendor/pygments-main/tests/examplefiles/ANTLRv3.g +608 -0
  147. data/vendor/pygments-main/tests/examplefiles/AcidStateAdvanced.hs +209 -0
  148. data/vendor/pygments-main/tests/examplefiles/AlternatingGroup.mu +102 -0
  149. data/vendor/pygments-main/tests/examplefiles/CPDictionary.j +611 -0
  150. data/vendor/pygments-main/tests/examplefiles/Constants.mo +158 -0
  151. data/vendor/pygments-main/tests/examplefiles/DancingSudoku.lhs +411 -0
  152. data/vendor/pygments-main/tests/examplefiles/Errors.scala +18 -0
  153. data/vendor/pygments-main/tests/examplefiles/File.hy +174 -0
  154. data/vendor/pygments-main/tests/examplefiles/Intro.java +1660 -0
  155. data/vendor/pygments-main/tests/examplefiles/Makefile +1131 -0
  156. data/vendor/pygments-main/tests/examplefiles/Object.st +4394 -0
  157. data/vendor/pygments-main/tests/examplefiles/OrderedMap.hx +584 -0
  158. data/vendor/pygments-main/tests/examplefiles/SmallCheck.hs +378 -0
  159. data/vendor/pygments-main/tests/examplefiles/Sorting.mod +470 -0
  160. data/vendor/pygments-main/tests/examplefiles/Sudoku.lhs +382 -0
  161. data/vendor/pygments-main/tests/examplefiles/addressbook.proto +30 -0
  162. data/vendor/pygments-main/tests/examplefiles/antlr_throws +1 -0
  163. data/vendor/pygments-main/tests/examplefiles/apache2.conf +393 -0
  164. data/vendor/pygments-main/tests/examplefiles/as3_test.as +143 -0
  165. data/vendor/pygments-main/tests/examplefiles/as3_test2.as +46 -0
  166. data/vendor/pygments-main/tests/examplefiles/as3_test3.as +3 -0
  167. data/vendor/pygments-main/tests/examplefiles/aspx-cs_example +27 -0
  168. data/vendor/pygments-main/tests/examplefiles/badcase.java +2 -0
  169. data/vendor/pygments-main/tests/examplefiles/batchfile.bat +49 -0
  170. data/vendor/pygments-main/tests/examplefiles/boot-9.scm +1557 -0
  171. data/vendor/pygments-main/tests/examplefiles/cells.ps +515 -0
  172. data/vendor/pygments-main/tests/examplefiles/ceval.c +2604 -0
  173. data/vendor/pygments-main/tests/examplefiles/cheetah_example.html +13 -0
  174. data/vendor/pygments-main/tests/examplefiles/classes.dylan +40 -0
  175. data/vendor/pygments-main/tests/examplefiles/condensed_ruby.rb +10 -0
  176. data/vendor/pygments-main/tests/examplefiles/coq_RelationClasses +447 -0
  177. data/vendor/pygments-main/tests/examplefiles/database.pytb +20 -0
  178. data/vendor/pygments-main/tests/examplefiles/de.MoinMoin.po +2461 -0
  179. data/vendor/pygments-main/tests/examplefiles/demo.ahk +181 -0
  180. data/vendor/pygments-main/tests/examplefiles/demo.cfm +38 -0
  181. data/vendor/pygments-main/tests/examplefiles/django_sample.html+django +68 -0
  182. data/vendor/pygments-main/tests/examplefiles/dwarf.cw +17 -0
  183. data/vendor/pygments-main/tests/examplefiles/erl_session +10 -0
  184. data/vendor/pygments-main/tests/examplefiles/escape_semicolon.clj +1 -0
  185. data/vendor/pygments-main/tests/examplefiles/evil_regex.js +48 -0
  186. data/vendor/pygments-main/tests/examplefiles/example.c +2080 -0
  187. data/vendor/pygments-main/tests/examplefiles/example.cls +15 -0
  188. data/vendor/pygments-main/tests/examplefiles/example.cpp +2363 -0
  189. data/vendor/pygments-main/tests/examplefiles/example.gs +106 -0
  190. data/vendor/pygments-main/tests/examplefiles/example.gst +7 -0
  191. data/vendor/pygments-main/tests/examplefiles/example.kt +47 -0
  192. data/vendor/pygments-main/tests/examplefiles/example.lua +250 -0
  193. data/vendor/pygments-main/tests/examplefiles/example.moo +26 -0
  194. data/vendor/pygments-main/tests/examplefiles/example.moon +629 -0
  195. data/vendor/pygments-main/tests/examplefiles/example.nim +1010 -0
  196. data/vendor/pygments-main/tests/examplefiles/example.ns2 +69 -0
  197. data/vendor/pygments-main/tests/examplefiles/example.p +34 -0
  198. data/vendor/pygments-main/tests/examplefiles/example.pas +2708 -0
  199. data/vendor/pygments-main/tests/examplefiles/example.rb +1852 -0
  200. data/vendor/pygments-main/tests/examplefiles/example.rhtml +561 -0
  201. data/vendor/pygments-main/tests/examplefiles/example.sh-session +19 -0
  202. data/vendor/pygments-main/tests/examplefiles/example.sml +156 -0
  203. data/vendor/pygments-main/tests/examplefiles/example.snobol +15 -0
  204. data/vendor/pygments-main/tests/examplefiles/example.tea +34 -0
  205. data/vendor/pygments-main/tests/examplefiles/example.u +548 -0
  206. data/vendor/pygments-main/tests/examplefiles/example.weechatlog +9 -0
  207. data/vendor/pygments-main/tests/examplefiles/example.xhtml +376 -0
  208. data/vendor/pygments-main/tests/examplefiles/example.yaml +302 -0
  209. data/vendor/pygments-main/tests/examplefiles/example2.aspx +29 -0
  210. data/vendor/pygments-main/tests/examplefiles/example_elixir.ex +363 -0
  211. data/vendor/pygments-main/tests/examplefiles/example_file.fy +128 -0
  212. data/vendor/pygments-main/tests/examplefiles/firefox.mak +586 -0
  213. data/vendor/pygments-main/tests/examplefiles/flipflop.sv +19 -0
  214. data/vendor/pygments-main/tests/examplefiles/foo.sce +6 -0
  215. data/vendor/pygments-main/tests/examplefiles/format.ml +1213 -0
  216. data/vendor/pygments-main/tests/examplefiles/fucked_up.rb +77 -0
  217. data/vendor/pygments-main/tests/examplefiles/function.mu +1 -0
  218. data/vendor/pygments-main/tests/examplefiles/functional.rst +1472 -0
  219. data/vendor/pygments-main/tests/examplefiles/genclass.clj +510 -0
  220. data/vendor/pygments-main/tests/examplefiles/genshi_example.xml+genshi +193 -0
  221. data/vendor/pygments-main/tests/examplefiles/genshitext_example.genshitext +33 -0
  222. data/vendor/pygments-main/tests/examplefiles/glsl.frag +7 -0
  223. data/vendor/pygments-main/tests/examplefiles/glsl.vert +13 -0
  224. data/vendor/pygments-main/tests/examplefiles/html+php_faulty.php +1 -0
  225. data/vendor/pygments-main/tests/examplefiles/http_request_example +14 -0
  226. data/vendor/pygments-main/tests/examplefiles/http_response_example +27 -0
  227. data/vendor/pygments-main/tests/examplefiles/import.hs +4 -0
  228. data/vendor/pygments-main/tests/examplefiles/intro.ik +24 -0
  229. data/vendor/pygments-main/tests/examplefiles/ints.php +10 -0
  230. data/vendor/pygments-main/tests/examplefiles/intsyn.fun +675 -0
  231. data/vendor/pygments-main/tests/examplefiles/intsyn.sig +286 -0
  232. data/vendor/pygments-main/tests/examplefiles/irb_heredoc +8 -0
  233. data/vendor/pygments-main/tests/examplefiles/irc.lsp +214 -0
  234. data/vendor/pygments-main/tests/examplefiles/java.properties +16 -0
  235. data/vendor/pygments-main/tests/examplefiles/jbst_example1.jbst +28 -0
  236. data/vendor/pygments-main/tests/examplefiles/jbst_example2.jbst +45 -0
  237. data/vendor/pygments-main/tests/examplefiles/jinjadesignerdoc.rst +713 -0
  238. data/vendor/pygments-main/tests/examplefiles/lighttpd_config.conf +13 -0
  239. data/vendor/pygments-main/tests/examplefiles/linecontinuation.py +47 -0
  240. data/vendor/pygments-main/tests/examplefiles/ltmain.sh +2849 -0
  241. data/vendor/pygments-main/tests/examplefiles/main.cmake +42 -0
  242. data/vendor/pygments-main/tests/examplefiles/markdown.lsp +679 -0
  243. data/vendor/pygments-main/tests/examplefiles/matlab_noreturn +3 -0
  244. data/vendor/pygments-main/tests/examplefiles/matlab_sample +27 -0
  245. data/vendor/pygments-main/tests/examplefiles/matlabsession_sample.txt +37 -0
  246. data/vendor/pygments-main/tests/examplefiles/minimal.ns2 +4 -0
  247. data/vendor/pygments-main/tests/examplefiles/moin_SyntaxReference.txt +340 -0
  248. data/vendor/pygments-main/tests/examplefiles/multiline_regexes.rb +38 -0
  249. data/vendor/pygments-main/tests/examplefiles/nasm_aoutso.asm +96 -0
  250. data/vendor/pygments-main/tests/examplefiles/nasm_objexe.asm +30 -0
  251. data/vendor/pygments-main/tests/examplefiles/nemerle_sample.n +87 -0
  252. data/vendor/pygments-main/tests/examplefiles/nginx_nginx.conf +118 -0
  253. data/vendor/pygments-main/tests/examplefiles/numbers.c +12 -0
  254. data/vendor/pygments-main/tests/examplefiles/objc_example.m +25 -0
  255. data/vendor/pygments-main/tests/examplefiles/objc_example2.m +24 -0
  256. data/vendor/pygments-main/tests/examplefiles/perl_misc +62 -0
  257. data/vendor/pygments-main/tests/examplefiles/perl_perl5db +998 -0
  258. data/vendor/pygments-main/tests/examplefiles/perl_regex-delims +120 -0
  259. data/vendor/pygments-main/tests/examplefiles/perlfunc.1 +856 -0
  260. data/vendor/pygments-main/tests/examplefiles/phpcomplete.vim +567 -0
  261. data/vendor/pygments-main/tests/examplefiles/pleac.in.rb +1223 -0
  262. data/vendor/pygments-main/tests/examplefiles/postgresql_test.txt +47 -0
  263. data/vendor/pygments-main/tests/examplefiles/pppoe.applescript +10 -0
  264. data/vendor/pygments-main/tests/examplefiles/psql_session.txt +122 -0
  265. data/vendor/pygments-main/tests/examplefiles/py3_test.txt +2 -0
  266. data/vendor/pygments-main/tests/examplefiles/pycon_test.pycon +14 -0
  267. data/vendor/pygments-main/tests/examplefiles/pytb_test2.pytb +2 -0
  268. data/vendor/pygments-main/tests/examplefiles/python25-bsd.mak +234 -0
  269. data/vendor/pygments-main/tests/examplefiles/qsort.prolog +13 -0
  270. data/vendor/pygments-main/tests/examplefiles/r-console-transcript.Rout +38 -0
  271. data/vendor/pygments-main/tests/examplefiles/ragel-cpp_rlscan +280 -0
  272. data/vendor/pygments-main/tests/examplefiles/ragel-cpp_snippet +2 -0
  273. data/vendor/pygments-main/tests/examplefiles/regex.js +22 -0
  274. data/vendor/pygments-main/tests/examplefiles/reversi.lsp +427 -0
  275. data/vendor/pygments-main/tests/examplefiles/ruby_func_def.rb +11 -0
  276. data/vendor/pygments-main/tests/examplefiles/scilab.sci +30 -0
  277. data/vendor/pygments-main/tests/examplefiles/sibling.prolog +19 -0
  278. data/vendor/pygments-main/tests/examplefiles/simple.md +747 -0
  279. data/vendor/pygments-main/tests/examplefiles/smarty_example.html +209 -0
  280. data/vendor/pygments-main/tests/examplefiles/source.lgt +343 -0
  281. data/vendor/pygments-main/tests/examplefiles/sources.list +62 -0
  282. data/vendor/pygments-main/tests/examplefiles/sphere.pov +18 -0
  283. data/vendor/pygments-main/tests/examplefiles/sqlite3.sqlite3-console +27 -0
  284. data/vendor/pygments-main/tests/examplefiles/squid.conf +30 -0
  285. data/vendor/pygments-main/tests/examplefiles/string.jl +1031 -0
  286. data/vendor/pygments-main/tests/examplefiles/string_delimiters.d +21 -0
  287. data/vendor/pygments-main/tests/examplefiles/stripheredoc.sh +3 -0
  288. data/vendor/pygments-main/tests/examplefiles/test.R +119 -0
  289. data/vendor/pygments-main/tests/examplefiles/test.adb +211 -0
  290. data/vendor/pygments-main/tests/examplefiles/test.asy +131 -0
  291. data/vendor/pygments-main/tests/examplefiles/test.awk +121 -0
  292. data/vendor/pygments-main/tests/examplefiles/test.bas +29 -0
  293. data/vendor/pygments-main/tests/examplefiles/test.bmx +145 -0
  294. data/vendor/pygments-main/tests/examplefiles/test.boo +39 -0
  295. data/vendor/pygments-main/tests/examplefiles/test.bro +250 -0
  296. data/vendor/pygments-main/tests/examplefiles/test.cs +374 -0
  297. data/vendor/pygments-main/tests/examplefiles/test.css +54 -0
  298. data/vendor/pygments-main/tests/examplefiles/test.d +135 -0
  299. data/vendor/pygments-main/tests/examplefiles/test.dart +23 -0
  300. data/vendor/pygments-main/tests/examplefiles/test.dtd +89 -0
  301. data/vendor/pygments-main/tests/examplefiles/test.ec +605 -0
  302. data/vendor/pygments-main/tests/examplefiles/test.ecl +58 -0
  303. data/vendor/pygments-main/tests/examplefiles/test.eh +315 -0
  304. data/vendor/pygments-main/tests/examplefiles/test.erl +169 -0
  305. data/vendor/pygments-main/tests/examplefiles/test.evoque +33 -0
  306. data/vendor/pygments-main/tests/examplefiles/test.fan +818 -0
  307. data/vendor/pygments-main/tests/examplefiles/test.flx +57 -0
  308. data/vendor/pygments-main/tests/examplefiles/test.gdc +13 -0
  309. data/vendor/pygments-main/tests/examplefiles/test.groovy +97 -0
  310. data/vendor/pygments-main/tests/examplefiles/test.html +339 -0
  311. data/vendor/pygments-main/tests/examplefiles/test.ini +10 -0
  312. data/vendor/pygments-main/tests/examplefiles/test.java +653 -0
  313. data/vendor/pygments-main/tests/examplefiles/test.jsp +24 -0
  314. data/vendor/pygments-main/tests/examplefiles/test.maql +45 -0
  315. data/vendor/pygments-main/tests/examplefiles/test.mod +374 -0
  316. data/vendor/pygments-main/tests/examplefiles/test.moo +51 -0
  317. data/vendor/pygments-main/tests/examplefiles/test.myt +166 -0
  318. data/vendor/pygments-main/tests/examplefiles/test.nim +93 -0
  319. data/vendor/pygments-main/tests/examplefiles/test.pas +743 -0
  320. data/vendor/pygments-main/tests/examplefiles/test.php +505 -0
  321. data/vendor/pygments-main/tests/examplefiles/test.plot +333 -0
  322. data/vendor/pygments-main/tests/examplefiles/test.ps1 +108 -0
  323. data/vendor/pygments-main/tests/examplefiles/test.pypylog +1839 -0
  324. data/vendor/pygments-main/tests/examplefiles/test.r3 +94 -0
  325. data/vendor/pygments-main/tests/examplefiles/test.rb +177 -0
  326. data/vendor/pygments-main/tests/examplefiles/test.rhtml +43 -0
  327. data/vendor/pygments-main/tests/examplefiles/test.scaml +8 -0
  328. data/vendor/pygments-main/tests/examplefiles/test.ssp +12 -0
  329. data/vendor/pygments-main/tests/examplefiles/test.tcsh +830 -0
  330. data/vendor/pygments-main/tests/examplefiles/test.vb +407 -0
  331. data/vendor/pygments-main/tests/examplefiles/test.vhdl +161 -0
  332. data/vendor/pygments-main/tests/examplefiles/test.xqy +138 -0
  333. data/vendor/pygments-main/tests/examplefiles/test.xsl +23 -0
  334. data/vendor/pygments-main/tests/examplefiles/truncated.pytb +15 -0
  335. data/vendor/pygments-main/tests/examplefiles/type.lisp +1202 -0
  336. data/vendor/pygments-main/tests/examplefiles/underscore.coffee +603 -0
  337. data/vendor/pygments-main/tests/examplefiles/unicode.applescript +5 -0
  338. data/vendor/pygments-main/tests/examplefiles/unicodedoc.py +11 -0
  339. data/vendor/pygments-main/tests/examplefiles/webkit-transition.css +3 -0
  340. data/vendor/pygments-main/tests/examplefiles/while.pov +13 -0
  341. data/vendor/pygments-main/tests/examplefiles/wiki.factor +384 -0
  342. data/vendor/pygments-main/tests/examplefiles/xml_example +1897 -0
  343. data/vendor/pygments-main/tests/examplefiles/zmlrpc.f90 +798 -0
  344. data/vendor/pygments-main/tests/old_run.py +138 -0
  345. data/vendor/pygments-main/tests/run.py +48 -0
  346. data/vendor/pygments-main/tests/support.py +15 -0
  347. data/vendor/pygments-main/tests/test_basic_api.py +294 -0
  348. data/vendor/pygments-main/tests/test_clexer.py +31 -0
  349. data/vendor/pygments-main/tests/test_cmdline.py +105 -0
  350. data/vendor/pygments-main/tests/test_examplefiles.py +97 -0
  351. data/vendor/pygments-main/tests/test_html_formatter.py +162 -0
  352. data/vendor/pygments-main/tests/test_latex_formatter.py +55 -0
  353. data/vendor/pygments-main/tests/test_perllexer.py +137 -0
  354. data/vendor/pygments-main/tests/test_regexlexer.py +47 -0
  355. data/vendor/pygments-main/tests/test_token.py +46 -0
  356. data/vendor/pygments-main/tests/test_using_api.py +40 -0
  357. data/vendor/pygments-main/tests/test_util.py +116 -0
  358. data/vendor/simplejson/.gitignore +10 -0
  359. data/vendor/simplejson/.travis.yml +5 -0
  360. data/vendor/simplejson/CHANGES.txt +291 -0
  361. data/vendor/simplejson/LICENSE.txt +19 -0
  362. data/vendor/simplejson/MANIFEST.in +5 -0
  363. data/vendor/simplejson/README.rst +19 -0
  364. data/vendor/simplejson/conf.py +179 -0
  365. data/vendor/simplejson/index.rst +628 -0
  366. data/vendor/simplejson/scripts/make_docs.py +18 -0
  367. data/vendor/simplejson/setup.py +104 -0
  368. data/vendor/simplejson/simplejson/__init__.py +510 -0
  369. data/vendor/simplejson/simplejson/_speedups.c +2745 -0
  370. data/vendor/simplejson/simplejson/decoder.py +425 -0
  371. data/vendor/simplejson/simplejson/encoder.py +567 -0
  372. data/vendor/simplejson/simplejson/ordered_dict.py +119 -0
  373. data/vendor/simplejson/simplejson/scanner.py +77 -0
  374. data/vendor/simplejson/simplejson/tests/__init__.py +67 -0
  375. data/vendor/simplejson/simplejson/tests/test_bigint_as_string.py +55 -0
  376. data/vendor/simplejson/simplejson/tests/test_check_circular.py +30 -0
  377. data/vendor/simplejson/simplejson/tests/test_decimal.py +66 -0
  378. data/vendor/simplejson/simplejson/tests/test_decode.py +83 -0
  379. data/vendor/simplejson/simplejson/tests/test_default.py +9 -0
  380. data/vendor/simplejson/simplejson/tests/test_dump.py +67 -0
  381. data/vendor/simplejson/simplejson/tests/test_encode_basestring_ascii.py +46 -0
  382. data/vendor/simplejson/simplejson/tests/test_encode_for_html.py +32 -0
  383. data/vendor/simplejson/simplejson/tests/test_errors.py +34 -0
  384. data/vendor/simplejson/simplejson/tests/test_fail.py +91 -0
  385. data/vendor/simplejson/simplejson/tests/test_float.py +19 -0
  386. data/vendor/simplejson/simplejson/tests/test_indent.py +86 -0
  387. data/vendor/simplejson/simplejson/tests/test_item_sort_key.py +20 -0
  388. data/vendor/simplejson/simplejson/tests/test_namedtuple.py +121 -0
  389. data/vendor/simplejson/simplejson/tests/test_pass1.py +76 -0
  390. data/vendor/simplejson/simplejson/tests/test_pass2.py +14 -0
  391. data/vendor/simplejson/simplejson/tests/test_pass3.py +20 -0
  392. data/vendor/simplejson/simplejson/tests/test_recursion.py +67 -0
  393. data/vendor/simplejson/simplejson/tests/test_scanstring.py +117 -0
  394. data/vendor/simplejson/simplejson/tests/test_separators.py +42 -0
  395. data/vendor/simplejson/simplejson/tests/test_speedups.py +20 -0
  396. data/vendor/simplejson/simplejson/tests/test_tuple.py +49 -0
  397. data/vendor/simplejson/simplejson/tests/test_unicode.py +109 -0
  398. data/vendor/simplejson/simplejson/tool.py +39 -0
  399. metadata +492 -0
@@ -0,0 +1,1223 @@
1
+ # -*- ruby -*-
2
+
3
+ # Local variables:
4
+ # indent-tabs-mode: nil
5
+ # ruby-indent-level: 4
6
+ # End:
7
+
8
+ # @@PLEAC@@_NAME
9
+ # @@SKIP@@ Ruby
10
+
11
+ # @@PLEAC@@_WEB
12
+ # @@SKIP@@ http://www.ruby-lang.org
13
+
14
+
15
+ # @@PLEAC@@_1.0
16
+ string = '\n' # two characters, \ and an n
17
+ string = 'Jon \'Maddog\' Orwant' # literal single quotes
18
+
19
+ string = "\n" # a "newline" character
20
+ string = "Jon \"Maddog\" Orwant" # literal double quotes
21
+
22
+ string = %q/Jon 'Maddog' Orwant/ # literal single quotes
23
+
24
+ string = %q[Jon 'Maddog' Orwant] # literal single quotes
25
+ string = %q{Jon 'Maddog' Orwant} # literal single quotes
26
+ string = %q(Jon 'Maddog' Orwant) # literal single quotes
27
+ string = %q<Jon 'Maddog' Orwant> # literal single quotes
28
+
29
+ a = <<"EOF"
30
+ This is a multiline here document
31
+ terminated by EOF on a line by itself
32
+ EOF
33
+
34
+
35
+ # @@PLEAC@@_1.1
36
+ value = string[offset,count]
37
+ value = string[offset..-1]
38
+
39
+ string[offset,count] = newstring
40
+ string[offset..-1] = newtail
41
+
42
+ # in Ruby we can also specify intervals by their two offsets
43
+ value = string[offset..offs2]
44
+ string[offset..offs2] = newstring
45
+
46
+ leading, s1, s2, trailing = data.unpack("A5 x3 A8 A8 A*")
47
+
48
+ fivers = string.unpack("A5" * (string.length/5))
49
+
50
+ chars = string.unpack("A1" * string.length)
51
+
52
+ string = "This is what you have"
53
+ # +012345678901234567890 Indexing forwards (left to right)
54
+ # 109876543210987654321- Indexing backwards (right to left)
55
+ # note that 0 means 10 or 20, etc. above
56
+
57
+ first = string[0, 1] # "T"
58
+ start = string[5, 2] # "is"
59
+ rest = string[13..-1] # "you have"
60
+ last = string[-1, 1] # "e"
61
+ end_ = string[-4..-1] # "have"
62
+ piece = string[-8, 3] # "you"
63
+
64
+ string[5, 2] = "wasn't" # change "is" to "wasn't"
65
+ string[-12..-1] = "ondrous" # "This wasn't wondrous"
66
+ string[0, 1] = "" # delete first character
67
+ string[-10..-1] = "" # delete last 10 characters
68
+
69
+ if string[-10..-1] =~ /pattern/
70
+ puts "Pattern matches in last 10 characters"
71
+ end
72
+
73
+ string[0, 5].gsub!(/is/, 'at')
74
+
75
+ a = "make a hat"
76
+ a[0, 1], a[-1, 1] = a[-1, 1], a[0, 1]
77
+
78
+ a = "To be or not to be"
79
+ b = a.unpack("x6 A6")
80
+
81
+ b, c = a.unpack("x6 A2 X5 A2")
82
+ puts "#{b}\n#{c}\n"
83
+
84
+ def cut2fmt(*args)
85
+ template = ''
86
+ lastpos = 1
87
+ for place in args
88
+ template += "A" + (place - lastpos).to_s + " "
89
+ lastpos = place
90
+ end
91
+ template += "A*"
92
+ return template
93
+ end
94
+
95
+ fmt = cut2fmt(8, 14, 20, 26, 30)
96
+
97
+
98
+ # @@PLEAC@@_1.2
99
+ # careful! "b is true" doesn't mean "b != 0" (0 is true in Ruby)
100
+ # thus no problem of "defined" later since only nil is false
101
+ # the following sets to `c' if `b' is nil or false
102
+ a = b || c
103
+
104
+ # if you need Perl's behaviour (setting to `c' if `b' is 0) the most
105
+ # effective way is to use Numeric#nonzero? (thanks to Dave Thomas!)
106
+ a = b.nonzero? || c
107
+
108
+ # you will still want to use defined? in order to test
109
+ # for scope existence of a given object
110
+ a = defined?(b) ? b : c
111
+
112
+ dir = ARGV.shift || "/tmp"
113
+
114
+
115
+ # @@PLEAC@@_1.3
116
+ v1, v2 = v2, v1
117
+
118
+ alpha, beta, production = %w(January March August)
119
+ alpha, beta, production = beta, production, alpha
120
+
121
+
122
+ # @@PLEAC@@_1.4
123
+ num = char[0]
124
+ char = num.chr
125
+
126
+ # Ruby also supports having a char from character constant
127
+ num = ?r
128
+
129
+ char = sprintf("%c", num)
130
+ printf("Number %d is character %c\n", num, num)
131
+
132
+ ascii = string.unpack("C*")
133
+ string = ascii.pack("C*")
134
+
135
+ hal = "HAL"
136
+ ascii = hal.unpack("C*")
137
+ # We can't use Array#each since we can't mutate a Fixnum
138
+ ascii.collect! { |i|
139
+ i + 1 # add one to each ASCII value
140
+ }
141
+ ibm = ascii.pack("C*")
142
+ puts ibm
143
+
144
+
145
+ # @@PLEAC@@_1.5
146
+ array = string.split('')
147
+
148
+ array = string.unpack("C*")
149
+
150
+ string.scan(/./) { |b|
151
+ # do something with b
152
+ }
153
+
154
+ string = "an apple a day"
155
+ print "unique chars are: ", string.split('').uniq.sort, "\n"
156
+
157
+ sum = 0
158
+ for ascval in string.unpack("C*") # or use Array#each for a pure OO style :)
159
+ sum += ascval
160
+ end
161
+ puts "sum is #{sum & 0xffffffff}" # since Ruby will go Bignum if necessary
162
+
163
+ # @@INCLUDE@@ include/ruby/slowcat.rb
164
+
165
+
166
+ # @@PLEAC@@_1.6
167
+ revbytes = string.reverse
168
+
169
+ revwords = string.split(" ").reverse.join(" ")
170
+
171
+ revwords = string.split(/(\s+)/).reverse.join
172
+
173
+ # using the fact that IO is Enumerable, you can directly "select" it
174
+ long_palindromes = File.open("/usr/share/dict/words").
175
+ select { |w| w.chomp!; w.reverse == w && w.length > 5 }
176
+
177
+
178
+ # @@PLEAC@@_1.7
179
+ while string.sub!("\t+") { ' ' * ($&.length * 8 - $`.length % 8) }
180
+ end
181
+
182
+
183
+ # @@PLEAC@@_1.8
184
+ 'You owe #{debt} to me'.gsub(/\#{(\w+)}/) { eval($1) }
185
+
186
+ rows, cols = 24, 80
187
+ text = %q(I am #{rows} high and #{cols} long)
188
+ text.gsub!(/\#{(\w+)}/) { eval("#{$1}") }
189
+ puts text
190
+
191
+ 'I am 17 years old'.gsub(/\d+/) { 2 * $&.to_i }
192
+
193
+
194
+ # @@PLEAC@@_1.9
195
+ e = "bo peep".upcase
196
+ e.downcase!
197
+ e.capitalize!
198
+
199
+ "thIS is a loNG liNE".gsub!(/\w+/) { $&.capitalize }
200
+
201
+
202
+ # @@PLEAC@@_1.10
203
+ "I have #{n+1} guanacos."
204
+ print "I have ", n+1, " guanacos."
205
+
206
+
207
+ # @@PLEAC@@_1.11
208
+ var = <<'EOF'.gsub(/^\s+/, '')
209
+ your text
210
+ goes here
211
+ EOF
212
+
213
+
214
+ # @@PLEAC@@_1.12
215
+ string = "Folding and splicing is the work of an editor,\n"+
216
+ "not a mere collection of silicon\n"+
217
+ "and\n"+
218
+ "mobile electrons!"
219
+
220
+ def wrap(str, max_size)
221
+ all = []
222
+ line = ''
223
+ for l in str.split
224
+ if (line+l).length >= max_size
225
+ all.push(line)
226
+ line = ''
227
+ end
228
+ line += line == '' ? l : ' ' + l
229
+ end
230
+ all.push(line).join("\n")
231
+ end
232
+
233
+ print wrap(string, 20)
234
+ #=> Folding and
235
+ #=> splicing is the
236
+ #=> work of an editor,
237
+ #=> not a mere
238
+ #=> collection of
239
+ #=> silicon and mobile
240
+ #=> electrons!
241
+
242
+
243
+ # @@PLEAC@@_1.13
244
+ string = %q(Mom said, "Don't do that.")
245
+ string.gsub(/['"]/) { '\\'+$& }
246
+ string.gsub(/['"]/, '\&\&')
247
+ string.gsub(/[^A-Z]/) { '\\'+$& }
248
+ "is a test!".gsub(/\W/) { '\\'+$& } # no function like quotemeta?
249
+
250
+
251
+ # @@PLEAC@@_1.14
252
+ string.strip!
253
+
254
+
255
+ # @@PLEAC@@_1.15
256
+ def parse_csv(text)
257
+ new = text.scan(/"([^\"\\]*(?:\\.[^\"\\]*)*)",?|([^,]+),?|,/)
258
+ new << nil if text[-1] == ?,
259
+ new.flatten.compact
260
+ end
261
+
262
+ line = %q<XYZZY,"","O'Reilly, Inc","Wall, Larry","a \"glug\" bit,",5,"Error, Core Dumped">
263
+ fields = parse_csv(line)
264
+ fields.each_with_index { |v,i|
265
+ print "#{i} : #{v}\n";
266
+ }
267
+
268
+
269
+ # @@PLEAC@@_1.16
270
+ # Use the soundex.rb Library from Michael Neumann.
271
+ # http://www.s-direktnet.de/homepages/neumann/rb_prgs/Soundex.rb
272
+ require 'Soundex'
273
+
274
+ code = Text::Soundex.soundex(string)
275
+ codes = Text::Soundex.soundex(array)
276
+
277
+ # substitution function for getpwent():
278
+ # returns an array of user entries,
279
+ # each entry contains the username and the full name
280
+ def login_names
281
+ result = []
282
+ File.open("/etc/passwd") { |file|
283
+ file.each_line { |line|
284
+ next if line.match(/^#/)
285
+ cols = line.split(":")
286
+ result.push([cols[0], cols[4]])
287
+ }
288
+ }
289
+ result
290
+ end
291
+
292
+ puts "Lookup user: "
293
+ user = STDIN.gets
294
+ user.chomp!
295
+ exit unless user
296
+ name_code = Text::Soundex.soundex(user)
297
+
298
+ splitter = Regexp.new('(\w+)[^,]*\b(\w+)')
299
+ for username, fullname in login_names do
300
+ firstname, lastname = splitter.match(fullname)[1,2]
301
+ if name_code == Text::Soundex.soundex(username)
302
+ || name_code == Text::Soundex.soundex(firstname)
303
+ || name_code == Text::Soundex.soundex(lastname)
304
+ then
305
+ puts "#{username}: #{firstname} #{lastname}"
306
+ end
307
+ end
308
+
309
+
310
+ # @@PLEAC@@_1.17
311
+ # @@INCLUDE@@ include/ruby/fixstyle.rb
312
+
313
+
314
+ # @@PLEAC@@_1.18
315
+ # @@INCLUDE@@ include/ruby/psgrep.rb
316
+
317
+
318
+ # @@PLEAC@@_2.1
319
+ # Matz tells that you can use Integer() for strict checked conversion.
320
+ Integer("abc")
321
+ #=> `Integer': invalid value for Integer: "abc" (ArgumentError)
322
+ Integer("567")
323
+ #=> 567
324
+
325
+ # You may use Float() for floating point stuff
326
+ Integer("56.7")
327
+ #=> `Integer': invalid value for Integer: "56.7" (ArgumentError)
328
+ Float("56.7")
329
+ #=> 56.7
330
+
331
+ # You may also use a regexp for that
332
+ if string =~ /^[+-]?\d+$/
333
+ p 'is an integer'
334
+ else
335
+ p 'is not'
336
+ end
337
+
338
+ if string =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/
339
+ p 'is a decimal number'
340
+ else
341
+ p 'is not'
342
+ end
343
+
344
+
345
+ # @@PLEAC@@_2.2
346
+ # equal(num1, num2, accuracy) : returns true if num1 and num2 are
347
+ # equal to accuracy number of decimal places
348
+ def equal(i, j, a)
349
+ sprintf("%.#{a}g", i) == sprintf("%.#{a}g", j)
350
+ end
351
+
352
+ wage = 536 # $5.36/hour
353
+ week = 40 * wage # $214.40
354
+ printf("One week's wage is: \$%.2f\n", week/100.0)
355
+
356
+
357
+ # @@PLEAC@@_2.3
358
+ num.round # rounds to integer
359
+
360
+ a = 0.255
361
+ b = sprintf("%.2f", a)
362
+ print "Unrounded: #{a}\nRounded: #{b}\n"
363
+ printf "Unrounded: #{a}\nRounded: %.2f\n", a
364
+
365
+ print "number\tint\tfloor\tceil\n"
366
+ a = [ 3.3 , 3.5 , 3.7, -3.3 ]
367
+ for n in a
368
+ printf("% .1f\t% .1f\t% .1f\t% .1f\n", # at least I don't fake my output :)
369
+ n, n.to_i, n.floor, n.ceil)
370
+ end
371
+
372
+
373
+ # @@PLEAC@@_2.4
374
+ def dec2bin(n)
375
+ [n].pack("N").unpack("B32")[0].sub(/^0+(?=\d)/, '')
376
+ end
377
+
378
+ def bin2dec(n)
379
+ [("0"*32+n.to_s)[-32..-1]].pack("B32").unpack("N")[0]
380
+ end
381
+
382
+
383
+ # @@PLEAC@@_2.5
384
+ for i in x .. y
385
+ # i is set to every integer from x to y, inclusive
386
+ end
387
+
388
+ x.step(y,7) { |i|
389
+ # i is set to every integer from x to y, stepsize = 7
390
+ }
391
+
392
+ print "Infancy is: "
393
+ (0..2).each { |i|
394
+ print i, " "
395
+ }
396
+ print "\n"
397
+
398
+
399
+ # @@PLEAC@@_2.6
400
+ # We can add conversion methods to the Integer class,
401
+ # this makes a roman number just a representation for normal numbers.
402
+ class Integer
403
+
404
+ @@romanlist = [["M", 1000],
405
+ ["CM", 900],
406
+ ["D", 500],
407
+ ["CD", 400],
408
+ ["C", 100],
409
+ ["XC", 90],
410
+ ["L", 50],
411
+ ["XL", 40],
412
+ ["X", 10],
413
+ ["IX", 9],
414
+ ["V", 5],
415
+ ["IV", 4],
416
+ ["I", 1]]
417
+
418
+ def to_roman
419
+ remains = self
420
+ roman = ""
421
+ for sym, num in @@romanlist
422
+ while remains >= num
423
+ remains -= num
424
+ roman << sym
425
+ end
426
+ end
427
+ roman
428
+ end
429
+
430
+ def Integer.from_roman(roman)
431
+ ustr = roman.upcase
432
+ sum = 0
433
+ for entry in @@romanlist
434
+ sym, num = entry[0], entry[1]
435
+ while sym == ustr[0, sym.length]
436
+ sum += num
437
+ ustr.slice!(0, sym.length)
438
+ end
439
+ end
440
+ sum
441
+ end
442
+
443
+ end
444
+
445
+
446
+ roman_fifteen = 15.to_roman
447
+ puts "Roman for fifteen is #{roman_fifteen}"
448
+ i = Integer.from_roman(roman_fifteen)
449
+ puts "Converted back, #{roman_fifteen} is #{i}"
450
+
451
+ # check
452
+ for i in (1..3900)
453
+ r = i.to_roman
454
+ j = Integer.from_roman(r)
455
+ if i != j
456
+ puts "error: #{i} : #{r} - #{j}"
457
+ end
458
+ end
459
+
460
+
461
+ # @@PLEAC@@_2.7
462
+ random = rand(y-x+1)+x
463
+
464
+ chars = ["A".."Z","a".."z","0".."9"].collect { |r| r.to_a }.join + %q(!@$%^&*)
465
+ password = (1..8).collect { chars[rand(chars.size)] }.pack("C*")
466
+
467
+
468
+ # @@PLEAC@@_2.8
469
+ srand # uses a combination of the time, the process id, and a sequence number
470
+ srand(val) # for repeatable behaviour
471
+
472
+
473
+ # @@PLEAC@@_2.9
474
+ # from the randomr lib:
475
+ # http://raa.ruby-lang.org/project/randomr/
476
+ ----> http://raa.ruby-lang.org/project/randomr/
477
+
478
+ require 'random/mersenne_twister'
479
+ mers = Random::MersenneTwister.new 123456789
480
+ puts mers.rand(0) # 0.550321932544541
481
+ puts mers.rand(10) # 2
482
+
483
+ # using online sources of random data via the realrand package:
484
+ # http://raa.ruby-lang.org/project/realrand/
485
+ # **Note**
486
+ # The following online services are used in this package:
487
+ # http://www.random.org - source: atmospheric noise
488
+ # http://www.fourmilab.ch/hotbits - source: radioactive decay timings
489
+ # http://random.hd.org - source: entropy from local and network noise
490
+ # Please visit the sites and respect the rules of each service.
491
+
492
+ require 'random/online'
493
+
494
+ generator1 = Random::RandomOrg.new
495
+ puts generator1.randbyte(5).join(",")
496
+ puts generator1.randnum(10, 1, 6).join(",") # Roll dice 10 times.
497
+
498
+ generator2 = Random::FourmiLab.new
499
+ puts generator2.randbyte(5).join(",")
500
+ # randnum is not supported.
501
+
502
+ generator3 = Random::EntropyPool.new
503
+ puts generator3.randbyte(5).join(",")
504
+ # randnum is not supported.
505
+
506
+
507
+ # @@PLEAC@@_2.10
508
+ def gaussian_rand
509
+ begin
510
+ u1 = 2 * rand() - 1
511
+ u2 = 2 * rand() - 1
512
+ w = u1*u1 + u2*u2
513
+ end while (w >= 1)
514
+ w = Math.sqrt((-2*Math.log(w))/w)
515
+ [ u2*w, u1*w ]
516
+ end
517
+
518
+ mean = 25
519
+ sdev = 2
520
+ salary = gaussian_rand[0] * sdev + mean
521
+ printf("You have been hired at \$%.2f\n", salary)
522
+
523
+
524
+ # @@PLEAC@@_2.11
525
+ def deg2rad(d)
526
+ (d/180.0)*Math::PI
527
+ end
528
+
529
+ def rad2deg(r)
530
+ (r/Math::PI)*180
531
+ end
532
+
533
+
534
+ # @@PLEAC@@_2.12
535
+ sin_val = Math.sin(angle)
536
+ cos_val = Math.cos(angle)
537
+ tan_val = Math.tan(angle)
538
+
539
+ # AFAIK Ruby's Math module doesn't provide acos/asin
540
+ # While we're at it, let's also define missing hyperbolic functions
541
+ module Math
542
+ def Math.asin(x)
543
+ atan2(x, sqrt(1 - x**2))
544
+ end
545
+ def Math.acos(x)
546
+ atan2(sqrt(1 - x**2), x)
547
+ end
548
+ def Math.atan(x)
549
+ atan2(x, 1)
550
+ end
551
+ def Math.sinh(x)
552
+ (exp(x) - exp(-x)) / 2
553
+ end
554
+ def Math.cosh(x)
555
+ (exp(x) + exp(-x)) / 2
556
+ end
557
+ def Math.tanh(x)
558
+ sinh(x) / cosh(x)
559
+ end
560
+ end
561
+
562
+ # The support for Complex numbers is not built-in
563
+ y = Math.acos(3.7)
564
+ #=> in `sqrt': square root for negative number (ArgumentError)
565
+
566
+ # There is an implementation of Complex numbers in 'complex.rb' in current
567
+ # Ruby distro, but it doesn't support atan2 with complex args, so it doesn't
568
+ # solve this problem.
569
+
570
+
571
+ # @@PLEAC@@_2.13
572
+ log_e = Math.log(val)
573
+ log_10 = Math.log10(val)
574
+
575
+ def log_base(base, val)
576
+ Math.log(val)/Math.log(base)
577
+ end
578
+
579
+ answer = log_base(10, 10_000)
580
+ puts "log10(10,000) = #{answer}"
581
+
582
+
583
+ # @@PLEAC@@_2.14
584
+ require 'matrix.rb'
585
+
586
+ a = Matrix[[3, 2, 3], [5, 9, 8]]
587
+ b = Matrix[[4, 7], [9, 3], [8, 1]]
588
+ c = a * b
589
+
590
+ a.row_size
591
+ a.column_size
592
+
593
+ c.det
594
+ a.transpose
595
+
596
+
597
+ # @@PLEAC@@_2.15
598
+ require 'complex.rb'
599
+ require 'rational.rb'
600
+
601
+ a = Complex(3, 5) # 3 + 5i
602
+ b = Complex(2, -2) # 2 - 2i
603
+ puts "c = #{a*b}"
604
+
605
+ c = a * b
606
+ d = 3 + 4*Complex::I
607
+
608
+ printf "sqrt(#{d}) = %s\n", Math.sqrt(d)
609
+
610
+
611
+ # @@PLEAC@@_2.16
612
+ number = hexadecimal.hex
613
+ number = octal.oct
614
+
615
+ print "Gimme a number in decimal, octal, or hex: "
616
+ num = gets.chomp
617
+ exit unless defined?(num)
618
+ num = num.oct if num =~ /^0/ # does both oct and hex
619
+ printf "%d %x %o\n", num, num, num
620
+
621
+ print "Enter file permission in octal: "
622
+ permissions = gets.chomp
623
+ raise "Exiting ...\n" unless defined?(permissions)
624
+ puts "The decimal value is #{permissions.oct}"
625
+
626
+
627
+ # @@PLEAC@@_2.17
628
+ def commify(n)
629
+ n.to_s =~ /([^\.]*)(\..*)?/
630
+ int, dec = $1.reverse, $2 ? $2 : ""
631
+ while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
632
+ end
633
+ int.reverse + dec
634
+ end
635
+
636
+
637
+ # @@PLEAC@@_2.18
638
+ printf "It took %d hour%s\n", time, time == 1 ? "" : "s"
639
+
640
+ # dunno if an equivalent to Lingua::EN::Inflect exists...
641
+
642
+
643
+ # @@PLEAC@@_2.19
644
+ #-----------------------------
645
+ #!/usr/bin/ruby
646
+ # bigfact - calculating prime factors
647
+ def factorize(orig)
648
+ factors = {}
649
+ factors.default = 0 # return 0 instead nil if key not found in hash
650
+ n = orig
651
+ i = 2
652
+ sqi = 4 # square of i
653
+ while sqi <= n do
654
+ while n.modulo(i) == 0 do
655
+ n /= i
656
+ factors[i] += 1
657
+ # puts "Found factor #{i}"
658
+ end
659
+ # we take advantage of the fact that (i +1)**2 = i**2 + 2*i +1
660
+ sqi += 2 * i + 1
661
+ i += 1
662
+ end
663
+
664
+ if (n != 1) && (n != orig)
665
+ factors[n] += 1
666
+ end
667
+ factors
668
+ end
669
+
670
+ def printfactorhash(orig, factorcount)
671
+ print format("%-10d ", orig)
672
+ if factorcount.length == 0
673
+ print "PRIME"
674
+ else
675
+ # sorts after number, because the hash keys are numbers
676
+ factorcount.sort.each { |factor,exponent|
677
+ print factor
678
+ if exponent > 1
679
+ print "**", exponent
680
+ end
681
+ print " "
682
+ }
683
+ end
684
+ puts
685
+ end
686
+
687
+ for arg in ARGV
688
+ n = arg.to_i
689
+ mfactors = factorize(n)
690
+ printfactorhash(n, mfactors)
691
+ end
692
+ #-----------------------------
693
+
694
+
695
+ # @@PLEAC@@_3.0
696
+ puts Time.now
697
+
698
+ print "Today is day ", Time.now.yday, " of the current year.\n"
699
+ print "Today is day ", Time.now.day, " of the current month.\n"
700
+
701
+
702
+ # @@PLEAC@@_3.1
703
+ day, month, year = Time.now.day, Time.now.month, Time.now.year
704
+ # or
705
+ day, month, year = Time.now.to_a[3..5]
706
+
707
+ tl = Time.now.localtime
708
+ printf("The current date is %04d %02d %02d\n", tl.year, tl.month, tl.day)
709
+
710
+ Time.now.localtime.strftime("%Y-%m-%d")
711
+
712
+
713
+ # @@PLEAC@@_3.2
714
+ Time.local(year, month, day, hour, minute, second).tv_sec
715
+ Time.gm(year, month, day, hour, minute, second).tv_sec
716
+
717
+
718
+ # @@PLEAC@@_3.3
719
+ sec, min, hour, day, month, year, wday, yday, isdst, zone = Time.at(epoch_secs).to_a
720
+
721
+
722
+ # @@PLEAC@@_3.4
723
+ when_ = now + difference # now -> Time ; difference -> Numeric (delta in seconds)
724
+ then_ = now - difference
725
+
726
+
727
+ # @@PLEAC@@_3.5
728
+ bree = 361535725
729
+ nat = 96201950
730
+
731
+ difference = bree - nat
732
+ puts "There were #{difference} seconds between Nat and Bree"
733
+
734
+ seconds = difference % 60
735
+ difference = (difference - seconds) / 60
736
+ minutes = difference % 60
737
+ difference = (difference - minutes) / 60
738
+ hours = difference % 24
739
+ difference = (difference - hours) / 24
740
+ days = difference % 7
741
+ weeks = (difference - days) / 7
742
+
743
+ puts "(#{weeks} weeks, #{days} days, #{hours}:#{minutes}:#{seconds})"
744
+
745
+
746
+ # @@PLEAC@@_3.6
747
+ monthday, weekday, yearday = date.mday, date.wday, date.yday
748
+
749
+ # AFAIK the week number is not just a division since week boundaries are on sundays
750
+ weeknum = d.strftime("%U").to_i + 1
751
+
752
+ year = 1981
753
+ month = "jun" # or `6' if you want to emulate a broken language
754
+ day = 16
755
+ t = Time.mktime(year, month, day)
756
+ print "#{month}/#{day}/#{year} was a ", t.strftime("%A"), "\n"
757
+
758
+
759
+ # @@PLEAC@@_3.7
760
+ yyyy, mm, dd = $1, $2, $3 if "1998-06-25" =~ /(\d+)-(\d+)-(\d+)/
761
+
762
+ epoch_seconds = Time.mktime(yyyy, mm, dd).tv_sec
763
+
764
+ # dunno an equivalent to Date::Manip#ParseDate
765
+
766
+
767
+ # @@PLEAC@@_3.8
768
+ string = Time.at(epoch_secs)
769
+ Time.at(1234567890).gmtime # gives: Fri Feb 13 23:31:30 UTC 2009
770
+
771
+ time = Time.mktime(1973, "jan", 18, 3, 45, 50)
772
+ print "In localtime it gives: ", time.localtime, "\n"
773
+
774
+
775
+ # @@PLEAC@@_3.9
776
+ # Ruby provides micro-seconds in Time object
777
+ Time.now.usec
778
+
779
+ # Ruby gives the seconds in floating format when substracting two Time objects
780
+ before = Time.now
781
+ line = gets
782
+ elapsed = Time.now - before
783
+ puts "You took #{elapsed} seconds."
784
+
785
+ # On my Celeron-400 with Linux-2.2.19-14mdk, average for three execs are:
786
+ # This Ruby version: average 0.00321 sec
787
+ # Cookbook's Perl version: average 0.00981 sec
788
+ size = 500
789
+ number_of_times = 100
790
+ total_time = 0
791
+ number_of_times.times {
792
+ # populate array
793
+ array = []
794
+ size.times { array << rand }
795
+ # sort it
796
+ begin_ = Time.now
797
+ array.sort!
798
+ time = Time.now - begin_
799
+ total_time += time
800
+ }
801
+ printf "On average, sorting %d random numbers takes %.5f seconds\n",
802
+ size, (total_time/Float(number_of_times))
803
+
804
+
805
+ # @@PLEAC@@_3.10
806
+ sleep(0.005) # Ruby is definitely not as broken as Perl :)
807
+ # (may be interrupted by sending the process a SIGALRM)
808
+
809
+
810
+ # @@PLEAC@@_3.11
811
+ #!/usr/bin/ruby -w
812
+ # hopdelta - feed mail header, produce lines
813
+ # showing delay at each hop.
814
+ require 'time'
815
+ class MailHopDelta
816
+
817
+ def initialize(mail)
818
+ @head = mail.gsub(/\n\s+/,' ')
819
+ @topline = %w-Sender Recipient Time Delta-
820
+ @start_from = mail.match(/^From.*\@([^\s>]*)/)[1]
821
+ @date = Time.parse(mail.match(/^Date:\s+(.*)/)[1])
822
+ end
823
+
824
+ def out(line)
825
+ "%-20.20s %-20.20s %-20.20s %s" % line
826
+ end
827
+
828
+ def hop_date(day)
829
+ day.strftime("%I:%M:%S %Y/%m/%d")
830
+ end
831
+
832
+ def puts_hops
833
+ puts out(@topline)
834
+ puts out(['Start', @start_from, hop_date(@date),''])
835
+ @head.split(/\n/).reverse.grep(/^Received:/).each do |hop|
836
+ hop.gsub!(/\bon (.*?) (id.*)/,'; \1')
837
+ whence = hop.match(/;\s+(.*)$/)[1]
838
+ unless whence
839
+ warn "Bad received line: #{hop}"
840
+ next
841
+ end
842
+ from = $+ if hop =~ /from\s+(\S+)|\((.*?)\)/
843
+ by = $1 if hop =~ /by\s+(\S+\.\S+)/
844
+ next unless now = Time.parse(whence).localtime
845
+ delta = now - @date
846
+ puts out([from, by, hop_date(now), hop_time(delta)])
847
+ @date = now
848
+ end
849
+ end
850
+
851
+ def hop_time(secs)
852
+ sign = secs < 0 ? -1 : 1
853
+ days, secs = secs.abs.divmod(60 * 60 * 24)
854
+ hours,secs = secs.abs.divmod(60 * 60)
855
+ mins, secs = secs.abs.divmod(60)
856
+ rtn = "%3ds" % [secs * sign]
857
+ rtn << "%3dm" % [mins * sign] if mins != 0
858
+ rtn << "%3dh" % [hours * sign] if hours != 0
859
+ rtn << "%3dd" % [days * sign] if days != 0
860
+ rtn
861
+ end
862
+ end
863
+
864
+ $/ = ""
865
+ mail = MailHopDelta.new(ARGF.gets).puts_hops
866
+
867
+
868
+ # @@PLEAC@@_4.0
869
+ single_level = [ "this", "that", "the", "other" ]
870
+
871
+ # Ruby directly supports nested arrays
872
+ double_level = [ "this", "that", [ "the", "other" ] ]
873
+ still_single_level = [ "this", "that", [ "the", "other" ] ].flatten
874
+
875
+
876
+ # @@PLEAC@@_4.1
877
+ a = [ "quick", "brown", "fox" ]
878
+ a = %w(Why are you teasing me?)
879
+
880
+ lines = <<"END_OF_HERE_DOC".gsub(/^\s*(.+)/, '\1')
881
+ The boy stood on the burning deck,
882
+ It was as hot as glass.
883
+ END_OF_HERE_DOC
884
+
885
+ bigarray = IO.readlines("mydatafile").collect { |l| l.chomp }
886
+
887
+ name = "Gandalf"
888
+ banner = %Q(Speak, #{name}, and welcome!)
889
+
890
+ host_info = `host #{his_host}`
891
+
892
+ %x(ps #{$$})
893
+
894
+ banner = 'Costs only $4.95'.split(' ')
895
+
896
+ rax = %w! ( ) < > { } [ ] !
897
+
898
+
899
+ # @@PLEAC@@_4.2
900
+ def commify_series(arr)
901
+ return '' if not arr
902
+ case arr.size
903
+ when 0 then ''
904
+ when 1 then arr[0]
905
+ when 2 then arr.join(' and ')
906
+ else arr[0..-2].join(', ') + ', and ' + arr[-1]
907
+ end
908
+ end
909
+
910
+ array = [ "red", "yellow", "green" ]
911
+
912
+ print "I have ", array, " marbles\n"
913
+ # -> I have redyellowgreen marbles
914
+
915
+ # But unlike Perl:
916
+ print "I have #{array} marbles\n"
917
+ # -> I have redyellowgreen marbles
918
+ # So, needs:
919
+ print "I have #{array.join(' ')} marbles\n"
920
+ # -> I have red yellow green marbles
921
+
922
+ #!/usr/bin/ruby
923
+ # communify_series - show proper comma insertion in list output
924
+
925
+ def commify_series(arr)
926
+ return '' if not arr
927
+ sepchar = arr.find { |p| p =~ /,/ } ? '; ' : ', '
928
+ case arr.size
929
+ when 0 then ''
930
+ when 1 then arr[0]
931
+ when 2 then arr.join(' and ')
932
+ else arr[0..-2].join(sepchar) + sepchar + 'and ' + arr[-1]
933
+ end
934
+ end
935
+
936
+ lists = [
937
+ [ 'just one thing' ],
938
+ %w(Mutt Jeff),
939
+ %w(Peter Paul Mary),
940
+ [ 'To our parents', 'Mother Theresa', 'God' ],
941
+ [ 'pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna' ],
942
+ [ 'recycle tired, old phrases', 'ponder big, happy thoughts' ],
943
+ [ 'recycle tired, old phrases',
944
+ 'ponder big, happy thoughts',
945
+ 'sleep and dream peacefully' ],
946
+ ]
947
+
948
+ for list in lists do
949
+ puts "The list is: #{commify_series(list)}."
950
+ end
951
+
952
+
953
+ # @@PLEAC@@_4.3
954
+ # (note: AFAIK Ruby doesn't allow gory change of Array length)
955
+ # grow the array by assigning nil to past the end of array
956
+ ary[new_size-1] = nil
957
+ # shrink the array by slicing it down
958
+ ary.slice!(new_size..-1)
959
+ # init the array with given size
960
+ Array.new(number_of_elems)
961
+ # assign to an element past the original end enlarges the array
962
+ ary[index_new_last_elem] = value
963
+
964
+ def what_about_that_array(a)
965
+ print "The array now has ", a.size, " elements.\n"
966
+ # Index of last element is not really interesting in Ruby
967
+ print "Element #3 is `#{a[3]}'.\n"
968
+ end
969
+ people = %w(Crosby Stills Nash Young)
970
+ what_about_that_array(people)
971
+
972
+
973
+ # @@PLEAC@@_4.4
974
+ # OO style
975
+ bad_users.each { |user|
976
+ complain(user)
977
+ }
978
+ # or, functional style
979
+ for user in bad_users
980
+ complain(user)
981
+ end
982
+
983
+ for var in ENV.keys.sort
984
+ puts "#{var}=#{ENV[var]}"
985
+ end
986
+
987
+ for user in all_users
988
+ disk_space = get_usage(user)
989
+ if (disk_space > MAX_QUOTA)
990
+ complain(user)
991
+ end
992
+ end
993
+
994
+ for l in IO.popen("who").readlines
995
+ print l if l =~ /^gc/
996
+ end
997
+
998
+ # we can mimic the obfuscated Perl way
999
+ while fh.gets # $_ is set to the line just read
1000
+ chomp # $_ has a trailing \n removed, if it had one
1001
+ split.each { |w| # $_ is split on whitespace
1002
+ # but $_ is not set to each chunk as in Perl
1003
+ print w.reverse
1004
+ }
1005
+ end
1006
+ # ...or use a cleaner way
1007
+ for l in fh.readlines
1008
+ l.chomp.split.each { |w| print w.reverse }
1009
+ end
1010
+
1011
+ # same drawback as in problem 1.4, we can't mutate a Numeric...
1012
+ array.collect! { |v| v - 1 }
1013
+
1014
+ a = [ .5, 3 ]; b = [ 0, 1 ]
1015
+ for ary in [ a, b ]
1016
+ ary.collect! { |v| v * 7 }
1017
+ end
1018
+ puts "#{a.join(' ')} #{b.join(' ')}"
1019
+
1020
+ # we can mutate Strings, cool; we need a trick for the scalar
1021
+ for ary in [ [ scalar ], array, hash.values ]
1022
+ ary.each { |v| v.strip! } # String#strip rules :)
1023
+ end
1024
+
1025
+
1026
+ # @@PLEAC@@_4.5
1027
+ # not relevant in Ruby since we have always references
1028
+ for item in array
1029
+ # do somethingh with item
1030
+ end
1031
+
1032
+
1033
+ # @@PLEAC@@_4.6
1034
+ unique = list.uniq
1035
+
1036
+ # generate a list of users logged in, removing duplicates
1037
+ users = `who`.collect { |l| l =~ /(\w+)/; $1 }.sort.uniq
1038
+ puts("users logged in: #{commify_series(users)}") # see 4.2 for commify_series
1039
+
1040
+
1041
+ # @@PLEAC@@_4.7
1042
+ a - b
1043
+ # [ 1, 1, 2, 2, 3, 3, 3, 4, 5 ] - [ 1, 2, 4 ] -> [3, 5]
1044
+
1045
+
1046
+ # @@PLEAC@@_4.8
1047
+ union = a | b
1048
+ intersection = a & b
1049
+ difference = a - b
1050
+
1051
+
1052
+ # @@PLEAC@@_4.9
1053
+ array1.concat(array2)
1054
+ # if you will assign to another object, better use:
1055
+ new_ary = array1 + array2
1056
+
1057
+ members = [ "Time", "Flies" ]
1058
+ initiates = [ "An", "Arrow" ]
1059
+ members += initiates
1060
+
1061
+ members = [ "Time", "Flies" ]
1062
+ initiates = [ "An", "Arrow" ]
1063
+ members[2,0] = [ "Like", initiates ].flatten
1064
+
1065
+ members[0] = "Fruit"
1066
+ members[3,2] = "A", "Banana"
1067
+
1068
+
1069
+ # @@PLEAC@@_4.10
1070
+ reversed = ary.reverse
1071
+
1072
+ ary.reverse_each { |e|
1073
+ # do something with e
1074
+ }
1075
+
1076
+ descending = ary.sort.reverse
1077
+ descending = ary.sort { |a,b| b <=> a }
1078
+
1079
+
1080
+ # @@PLEAC@@_4.11
1081
+ # remove n elements from front of ary (shift n)
1082
+ front = ary.slice!(0, n)
1083
+
1084
+ # remove n elements from the end of ary (pop n)
1085
+ end_ = ary.slice!(-n .. -1)
1086
+
1087
+ # let's extend the Array class, to make that useful
1088
+ class Array
1089
+ def shift2()
1090
+ slice!(0 .. 1) # more symetric with pop2...
1091
+ end
1092
+ def pop2()
1093
+ slice!(-2 .. -1)
1094
+ end
1095
+ end
1096
+
1097
+ friends = %w(Peter Paul Mary Jim Tim)
1098
+ this, that = friends.shift2
1099
+
1100
+ beverages = %w(Dew Jolt Cola Sprite Fresca)
1101
+ pair = beverages.pop2
1102
+
1103
+
1104
+ # @@PLEAC@@_4.12
1105
+ # use Enumerable#detect (or the synonym Enumerable#find)
1106
+ highest_eng = employees.detect { |emp| emp.category == 'engineer' }
1107
+
1108
+
1109
+ # @@PLEAC@@_4.13
1110
+ # use Enumerable#select (or the synonym Enumerable#find_all)
1111
+ bigs = nums.select { |i| i > 1_000_000 }
1112
+ pigs = users.keys.select { |k| users[k] > 1e7 }
1113
+
1114
+ matching = `who`.select { |u| u =~ /^gnat / }
1115
+
1116
+ engineers = employees.select { |e| e.position == 'Engineer' }
1117
+
1118
+ secondary_assistance = applicants.select { |a|
1119
+ a.income >= 26_000 && a.income < 30_000
1120
+ }
1121
+
1122
+
1123
+ # @@PLEAC@@_4.14
1124
+ # normally you would have an array of Numeric (Float or
1125
+ # Fixnum or Bignum), so you would use:
1126
+ sorted = unsorted.sort
1127
+ # if you have strings representing Integers or Floats
1128
+ # you may specify another sort method:
1129
+ sorted = unsorted.sort { |a,b| a.to_f <=> b.to_f }
1130
+
1131
+ # let's use the list of my own PID's
1132
+ `ps ux`.split("\n")[1..-1].
1133
+ select { |i| i =~ /^#{ENV['USER']}/ }.
1134
+ collect { |i| i.split[1] }.
1135
+ sort { |a,b| a.to_i <=> b.to_i }.each { |i| puts i }
1136
+ puts "Select a process ID to kill:"
1137
+ pid = gets.chomp
1138
+ raise "Exiting ... \n" unless pid && pid =~ /^\d+$/
1139
+ Process.kill('TERM', pid.to_i)
1140
+ sleep 2
1141
+ Process.kill('KILL', pid.to_i)
1142
+
1143
+ descending = unsorted.sort { |a,b| b.to_f <=> a.to_f }
1144
+
1145
+
1146
+ # @@PLEAC@@_4.15
1147
+ ordered = unordered.sort { |a,b| compare(a,b) }
1148
+
1149
+ precomputed = unordered.collect { |e| [compute, e] }
1150
+ ordered_precomputed = precomputed.sort { |a,b| a[0] <=> b[0] }
1151
+ ordered = ordered_precomputed.collect { |e| e[1] }
1152
+
1153
+ ordered = unordered.collect { |e| [compute, e] }.
1154
+ sort { |a,b| a[0] <=> b[0] }.
1155
+ collect { |e| e[1] }
1156
+
1157
+ for employee in employees.sort { |a,b| a.name <=> b.name }
1158
+ print employee.name, " earns \$ ", employee.salary, "\n"
1159
+ end
1160
+
1161
+ # Beware! `0' is true in Ruby.
1162
+ # For chaining comparisons, you may use Numeric#nonzero?, which
1163
+ # returns num if num is not zero, nil otherwise
1164
+ sorted = employees.sort { |a,b| (a.name <=> b.name).nonzero? || b.age <=> a.age }
1165
+
1166
+ users = []
1167
+ # getpwent is not wrapped in Ruby... let's fallback
1168
+ IO.readlines('/etc/passwd').each { |u| users << u.split(':') }
1169
+ users.sort! { |a,b| a[0] <=> b[0] }
1170
+ for user in users
1171
+ puts user[0]
1172
+ end
1173
+
1174
+ sorted = names.sort { |a,b| a[1, 1] <=> b[1, 1] }
1175
+ sorted = strings.sort { |a,b| a.length <=> b.length }
1176
+
1177
+ # let's show only the compact version
1178
+ ordered = strings.collect { |e| [e.length, e] }.
1179
+ sort { |a,b| a[0] <=> b[0] }.
1180
+ collect { |e| e[1] }
1181
+
1182
+ ordered = strings.collect { |e| [/\d+/.match(e)[0].to_i, e] }.
1183
+ sort { |a,b| a[0] <=> b[0] }.
1184
+ collect { |e| e[1] }
1185
+
1186
+ print `cat /etc/passwd`.collect { |e| [e, e.split(':').indexes(3,2,0)].flatten }.
1187
+ sort { |a,b| (a[1] <=> b[1]).nonzero? || (a[2] <=> b[2]).nonzero? || a[3] <=> b[3] }.
1188
+ collect { |e| e[0] }
1189
+
1190
+
1191
+ # @@PLEAC@@_4.16
1192
+ circular.unshift(circular.pop) # the last shall be first
1193
+ circular.push(circular.shift) # and vice versa
1194
+
1195
+ def grab_and_rotate(l)
1196
+ l.push(ret = l.shift)
1197
+ ret
1198
+ end
1199
+
1200
+ processes = [1, 2, 3, 4, 5]
1201
+ while (1)
1202
+ process = grab_and_rotate(processes)
1203
+ puts "Handling process #{process}"
1204
+ sleep 1
1205
+ end
1206
+
1207
+
1208
+ # @@PLEAC@@_4.17
1209
+ def fisher_yates_shuffle(a)
1210
+ (a.size-1).downto(1) { |i|
1211
+ j = rand(i+1)
1212
+ a[i], a[j] = a[j], a[i] if i != j
1213
+ }
1214
+ end
1215
+
1216
+ def naive_shuffle(a)
1217
+ for i in 0...a.size
1218
+ j = rand(a.size)
1219
+ a[i], a[j] = a[j], a[i]
1220
+ end
1221
+ end
1222
+
1223
+