{"id":2556,"date":"2021-11-19T02:03:48","date_gmt":"2021-11-19T02:03:48","guid":{"rendered":"https:\/\/dft.wiki\/?p=2556"},"modified":"2026-06-08T22:19:13","modified_gmt":"2026-06-09T02:19:13","slug":"ruby-cheat-sheet","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2556","title":{"rendered":"Ruby Cheat Sheet"},"content":{"rendered":"<p>On the official website, Ruby is described as &#8220;A dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write&#8221;, which is accurate.<\/p>\n<p>Ruby has been losing popularity to Python despite sharing many of the same characteristics, but it remains widely adopted by the Linux and hacking communities (many exploits were written in Ruby).<\/p>\n<p><strong>Working with strings:<\/strong><\/p>\n<pre>varName = \"!\"\r\nintName = 5\r\nprint (\"String\" + varName)\r\nputs (\"String\" + intName.to_s)<\/pre>\n<p><strong>Booleans and Numbers:<\/strong><\/p>\n<pre>booleanName = true\r\nintName = -99\r\nintName += 9\r\nputs intName.abs()<\/pre>\n<p><strong>Float (decimals):<\/strong><\/p>\n<pre>floatName = 0.00001\r\nputs floatName.round()<\/pre>\n<p><strong>Absence of a value:<\/strong><\/p>\n<pre>notAssignedValue = nil<\/pre>\n<p><strong>Escaping characters, changing cases, getting information, and indexes:<\/strong><\/p>\n<pre>varName = \"Test print \\\".\"\r\nputs varName.upcase()\r\nvarName = \"New line.\\n\"\r\nputs varName.downcase()\r\nvarName = \"     Extra spaces.     \"\r\nputs varName.strip()\r\nputs varName.length()\r\nputs varName.include? \"Extra\"\r\nputs varName.index(\"E\")\r\nputs varName[5]\r\nputs varName[5,5]<\/pre>\n<p><strong>Math operations:<\/strong><\/p>\n<pre>puts Math.sqrt(64)\r\nputs Math.log(10)<\/pre>\n<p><strong>Prompting for input and converting to numbers:<\/strong><\/p>\n<pre>strName = gets()\r\nstrName = gets.chomp()\r\nputs strName.to_i\r\nputs strName.to_f<\/pre>\n<p><strong>Working with arrays:<\/strong><\/p>\n<pre>arrayName = [\"A\",\"B\",\"C\"]\r\nputs arrayName\r\narrayName = Array.new\r\narrayName[0] = \"a\"\r\nputs arrayName.length()\r\nputs arrayName.include? \"a\"\r\nputs arrayName.sort()\r\nputs arrayName.reverse()<\/pre>\n<p><strong>Dictionary \/ Hash:<\/strong><\/p>\n<pre>hashName = {\r\n  \"A\" =&gt; \"X\",\r\n  \"B\" =&gt; \"Y\",\r\n  \"C\" =&gt; \"Z\"\r\n}\r\nputs hashName[\"B\"]<\/pre>\n<p><strong>Creating and calling functions with an IF statement:<\/strong><\/p>\n<pre>def methodName(message, print=false)\r\n  if print and message\r\n    puts message\r\n  elsif !message\r\n    puts \"no message\"\r\n  else\r\n    puts \"not set to print\"\r\n  end\r\n  return \"first\", \"second\"\r\nend\r\nputs methodName(\"Hi!\")\r\nputs methodName(\"Hi!\",1)\r\nputs methodName(false,1)<\/pre>\n<p><strong>While looping:<\/strong><\/p>\n<pre>while true\r\n  puts \"Executed!\"\r\n  break\r\nend<\/pre>\n<p><strong>Looping through a list:<\/strong><\/p>\n<pre>listName = [\"M\",\"N\",\"O\"]\r\nfor line in listName\r\n  puts line\r\nend\r\nlistName.each do |line|\r\n  puts line\r\nend<\/pre>\n<p><strong>For looping:<\/strong><\/p>\n<pre>for i in 1..3\r\n  puts i\r\nend\r\n3.times do |i|\r\n  puts i\r\nend<\/pre>\n<p><strong>Handling errors:<\/strong><\/p>\n<pre>begin\r\n  x = 1 \/ 0\r\nrescue ZeroDivisionError\r\n  puts \"Impossible to divide by Zero\"\r\nrescue TypeError =&gt; e\r\n  puts e\r\nend<\/pre>\n<p><strong>Classes and objects:<\/strong><\/p>\n<pre>class ClassName\r\n  attr_accessor :attr1, :attr2, :attr3\r\nend\r\nobjName = ClassName.new()\r\nobjName.attr1 = \"Text\"\r\nobjName.attr2 = 10\r\nobjName.attr3 = 0.5\r\nputs (objName.attr1 + objName.attr2.to_s + objName.attr3.to_s)<\/pre>\n<p><strong>Classes, attributes, and constructors:<\/strong><\/p>\n<pre>class ClassName2\r\n  attr_accessor :attr1, :attr2, :attr3\r\n  def initialize(attr1, attr2, attr3)\r\n    @attr1 = attr1\r\n    @attr2 = attr2.to_i\r\n    @attr3 = attr3.to_f\r\n  end\r\n  def print\r\n    puts (@attr1 + @attr2.to_s + @attr3.to_s)\r\n  end\r\nend\r\nobjName2 = ClassName2.new(\"Text\",10,0.5)\r\nobjName2.print<\/pre>\n<p><strong>Parent and child classes:<\/strong><\/p>\n<pre>class ClassName3 &lt; ClassName\r\n  def print\r\n    puts (@attr1 + @attr2.to_s + @attr3.to_s)\r\n  end\r\nend\r\nobjName3 = ClassName3.new()\r\nobjName3.attr1 = \"Text\"\r\nobjName3.attr2 = 10\r\nobjName3.attr3 = 0.5\r\nobjName3.print<\/pre>\n<p><strong>Creating modules (libraries) of functions:<\/strong><\/p>\n<pre>module ModuleName\r\n  def functionName\r\n    puts \"Executed!\"\r\n  end\r\nend\r\ninclude ModuleName\r\nModuleName.functionName()<\/pre>\n<p><strong>Importing\/requiring functions from a module:<\/strong><\/p>\n<pre>require \"\/home\/user\/file.rb\"\r\nrequire_relative \"file.rb\"\r\ninclude ModuleName\r\nModuleName.functionName()<\/pre>\n<p><strong>Interactive Ruby:<\/strong><\/p>\n<pre>irb -v\r\nirb<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>On the official website, Ruby is described as &#8220;A dynamic, open-source programming language with a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2556","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2556"}],"version-history":[{"count":3,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2556\/revisions"}],"predecessor-version":[{"id":5704,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2556\/revisions\/5704"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}