\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{pixelus} \pdfinfo{ /Title (ruby-essentials.pdf) /Creator (Cheatography) /Author (pixelus) /Subject (Ruby Essentials Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A91201} \definecolor{LightBackground}{HTML}{FCF7F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Ruby Essentials Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{pixelus} via \textcolor{DarkBackground}{\uline{cheatography.com/125267/cs/24072/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}pixelus \\ \uline{cheatography.com/pixelus} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 25th August, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Generalities}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}Everything in Ruby is an object.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}A method always return exactly one single thing (an object).} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}We can use do and end inplace of \{ \}.} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}Create a comment with \#.} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Printing Data}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}print "string" & print data to the screen without adding a new line at the end \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}puts "string" & print data to the screen and add an automatic new line \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}p "string" & print data to the screen with a new line and give information on type of data \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Special Characters}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}\textbackslash{}n & add a new line \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \{\{nobreak\}\}\textbackslash{}t & add a tabulation \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.84 cm} x{4.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Conversions}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}string.split(" ") & convert a string into an array \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}array.join(" ") & convert an array into a string \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}string.to\_i & convert a string to an integer \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \{\{nobreak\}\}element.to\_s & convert an element to a string \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}element.to\_a & convert an element to an array \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \{\{nobreak\}\}num.to\_f & convert an integer to a float \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Ranges}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}(start..end).each \{ |ele| … \} & specify a range of numbers including end \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}(start...end).each \{ |ele| … \} & specify a range of numbers excluding end \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Iterators}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}number.times \{…\} & repeat a block a number of times \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}array.each \{ |ele| ... \} & iterate over element of an array \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}array.each\_with\_index \{ |ele, i| ... \} & iterate over elements of an array with index \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \{\{nobreak\}\}string.each\_char \{ |char| ... \} & iterate over characters of a string \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}string.each\_char.with\_index \{ |char, i| ... \} & iterate over characters of a string with index \tn % Row Count 14 (+ 4) % Row 5 \SetRowColor{white} \{\{nobreak\}\}hash.each \{ |key, val| ... \} & iterate over elements of a hash \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}hash.each\_key \{ |key| ... \} & iterate over keys of a hash \tn % Row Count 18 (+ 2) % Row 7 \SetRowColor{white} \{\{nobreak\}\}hash.each\_value \{ |val| ... \} & iterate over values of a hash \tn % Row Count 21 (+ 3) % Row 8 \SetRowColor{LightBackground} \{\{nobreak\}\}element.inject \{ |acc, el| acc + el \} & return the value for the method where each element of the block is passed in an accumulator value and the current element \tn % Row Count 28 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.2 cm} x{4.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}array.push & add element(s) to the end of an array \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}array.unshift & add element(s) to the front of an array \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}array.pop & remove the last element of an array \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \{\{nobreak\}\}array.shift & remove the first element of an array \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}array.include?() & check if an element exists in an array \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \{\{nobreak\}\}array.index & find the index of an element in an array \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Objects}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}element.is\_a? (Object) & return true if class is the class of Object, or if class is one of the superclasses of Object or modules included in Object \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \{\{nobreak\}\}element.object\_id & return the memory address of some data \tn % Row Count 9 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}prc = Proc.new \{ |ele| ele * 2 \} & proc, an object that contains a block and allow to save blocks to variables \tn % Row Count 13 (+ 4) % Row 3 \SetRowColor{white} \{\{nobreak\}\}p prc.call(5) & call the proc and evaluate to the last line of code executed within the block \tn % Row Count 17 (+ 4) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}\&prc & convert a block into a proc or convert a proc into a block, \& in the parameters for a method definition will convert a block to a proc, \& in the arguments for a method call will convert a proc to a block \tn % Row Count 28 (+ 11) % Row 5 \SetRowColor{white} \{\{nobreak\}\}obj.instance\_of? Class & return true if obj is an instance of the given class \tn % Row Count 31 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{If we don't intend to mutate a string, we can use a symbol to save some memory because a symbol value will be stored in exactly one memory location. So they are often used to act as unique identifiers.}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Enumerables}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}array.all? \{ |ele| … \} & return true when all elements result in true \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}array.any? \{ |ele| … \} & return true when at least one element result in true \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}array.none? \{ |ele| … \} & return true when no element result in true \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \{\{nobreak\}\}array.one? \{ |ele| … \} & return true when exactly one element result in true \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}array.map \{ |ele| ... \} & return a new array containing the values returned by the block \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} \{\{nobreak\}\}array.sum & return the total sum of all elements \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}array.min & return the minimum element \tn % Row Count 18 (+ 2) % Row 7 \SetRowColor{white} \{\{nobreak\}\}array.max & return the maximum element \tn % Row Count 20 (+ 2) % Row 8 \SetRowColor{LightBackground} \{\{nobreak\}\}array.flatten & return the 1 dimensional version of any multidimensional array \tn % Row Count 23 (+ 3) % Row 9 \SetRowColor{white} \{\{nobreak\}\}array.select & return an array containing all elements of enum for which the given block returns a true value \tn % Row Count 28 (+ 5) % Row 10 \SetRowColor{LightBackground} \{\{nobreak\}\}element.length & return a number representing the length of the element \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Enumerables (cont)}} \tn % Row 11 \SetRowColor{LightBackground} \{\{nobreak\}\}element.count & return a number representing the count of elements that result in true \tn % Row Count 4 (+ 4) % Row 12 \SetRowColor{white} \{\{nobreak\}\}element.last & return the last element of a string or an array \tn % Row Count 7 (+ 3) % Row 13 \SetRowColor{LightBackground} \{\{nobreak\}\}element.last(num) & return a substring from the end of the string until it reaches the num value (counting backwards), or return a copy if the given limit is greater than or equal to the string length \tn % Row Count 16 (+ 9) % Row 14 \SetRowColor{white} \{\{nobreak\}\}num.even? & return true if the number is even \tn % Row Count 18 (+ 2) % Row 15 \SetRowColor{LightBackground} \{\{nobreak\}\}num.odd? & return true if the number is odd \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Inpout / Output}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}require File & import File when gems are involved \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}require\_relative File & import File with a path to another ruby file \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}element = gets & allow a user to give input and add a newline character at the end \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Scope}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}\$message = "something" & create a global variable, everywhere area in the code can access the global scope \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \{\{nobreak\}\}\$PROGRAM\_NAME & global variable, string describing the name of the program \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}\$stdin & global variable that holds a stream for the standard input \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} \{\{nobreak\}\}\$stdout & global variable which holds the standard output stream \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}CONSTANT & constant variable, cannot be reassigned and begin the name with a capital letter \tn % Row Count 17 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}string.downcase & return a copy of str with all downcase letters \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}string.upcase & return a copy of str with all uppercase letters \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}string.c​ap​italize & return a copy of str with the first character converted to uppercase \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} \{\{nobreak\}\}string.reverse & return a new string with the characters in reverse order \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}string.chomp & remove the last character if it's a newline or carriage return \tn % Row Count 17 (+ 4) % Row 5 \SetRowColor{white} \{\{nobreak\}\}string.index​(po​sition) & return the character in the specified position \tn % Row Count 20 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Hashes}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}hash = Hash.new OR hash = \{\} & create a new hash \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}hash = Hash.new(...) & create a new hash with a default value \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}Hash.new \{ |h, k| h{[}k{]} = {[}{]} \} & create a new hash and define default value as an array \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \{\{nobreak\}\}hash{[}key{]} & return value of the hash key \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}hash.has\_key?() & check if a key exists in a hash \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \{\{nobreak\}\}hash.has\_value?() & check if a value exists in a hash \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}hash.sort\_by \{|k, v| v\} & return the value in ascending order in a new array \tn % Row Count 16 (+ 3) % Row 7 \SetRowColor{white} \{\{nobreak\}\}hash.sort\_by \{|k, v| -v\} & return the value in descending order in a new array \tn % Row Count 19 (+ 3) % Row 8 \SetRowColor{LightBackground} \{\{nobreak\}\}hash.sort\_by \{|k, v| {[}-v, k{]}\} & sort the hash by the values in descending order and sort it by letter's alphabetical order in a new array \tn % Row Count 25 (+ 6) % Row 9 \SetRowColor{white} \{\{nobreak\}\}\{ |a, b| a.to\_s \textless{}=\textgreater{} b.to\_s \} & sort the numbers alphabetically by characters \tn % Row Count 28 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Symbols}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}\&:symbol & turn the symbol into a simple proc, equivalent to: element.method \{ |ele| ele.symbol\_method \} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \{\{nobreak\}\}hash = \{:k1 =\textgreater{} "v1", :k2=\textgreater{} "v2"\} & initializing a hash with symbol keys allows to drop the rocket (=\textgreater{}) and move the colon (:) to the right of the symbol \tn % Row Count 11 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Symbols are immutable and can never been changed.}} \newline {\bf{If we don't intend to mutate a string, we can use a symbol to save some memory because a symbol value will be stored in exactly one memory location. So they are often used to act as unique identifiers.}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}def method(arg\_1, arg\_2, *other\_args) & accept additional arguments and stock them into an array \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}method(*array) & pass an array into a function expecting multiple arguments \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}method(**hash) & pass a hash into a function expecting multiple arguments \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \{\{nobreak\}\}array = {[}*arr\_1, element, *arr\_2{]} & decompose an array into individual items where each individual element become an argument \tn % Row Count 14 (+ 5) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}hash = {[}*some\_hash, symbol: value{]} & decompose a hash into individual items where each individual element become an argument, only work with hashes where the keys are symbols \tn % Row Count 21 (+ 7) % Row 5 \SetRowColor{white} \{\{nobreak\}\}element\_1 \textless{}=\textgreater{} element\_2 & compare two values and return -1, 0, or 1 \tn % Row Count 24 (+ 3) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}a ||= b & assign b to a iff a is nil or false \tn % Row Count 26 (+ 2) % Row 7 \SetRowColor{white} \{\{nobreak\}\}a \&\&= b & assign b to a if a is true or not nil \tn % Row Count 28 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Class}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}initialize & put define default argument \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}@variable & d inside \#initializeinstance variable or attribute of class, typically assigned inside \#initialize, changing the variable will only effect that one instance \tn % Row Count 9 (+ 7) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}@@variable & class variable, typically assigned inside of the class, but not inside of \#initialize, changing the variable will effect all instances because all instances of the class \tn % Row Count 17 (+ 8) % Row 3 \SetRowColor{white} \{\{nobreak\}\}CLASS\_CONSTANT & class constant, will be shared among all instances of a class, but cannot be changed \tn % Row Count 21 (+ 4) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}attr\_r​eader & instance variable getter \tn % Row Count 23 (+ 2) % Row 5 \SetRowColor{white} \{\{nobreak\}\}attr\_writer & instance variable setter \tn % Row Count 25 (+ 2) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}attr\_a​ccesor & instance variable getter and setter \tn % Row Count 27 (+ 2) % Row 7 \SetRowColor{white} \{\{nobreak\}\}Class.new & create a new anonymous (unnamed) class \tn % Row Count 29 (+ 2) % Row 8 \SetRowColor{LightBackground} \{\{nobreak\}\}def method & instance method we can only call it on a Class instance we initialized using Class.new, instance method depends on the attributes or instance variables of an instance \tn % Row Count 36 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Class (cont)}} \tn % Row 9 \SetRowColor{LightBackground} \{\{nobreak\}\}def self.method & class method called directly on the class, self refers to the Class itself and cannot refer to any instance attributes like @variable (Class::method) \tn % Row Count 7 (+ 7) % Row 10 \SetRowColor{white} \{\{nobreak\}\}Queue.new & create a queue, process work in FIFO (first-in-first-out) order \tn % Row Count 10 (+ 3) % Row 11 \SetRowColor{LightBackground} \{\{nobreak\}\}Class::CONSTANT & access to the constant inside the class \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{To create a class we use the class keyword.}} \newline {\bf{The name of a class must begin with a capital letter.}} \newline {\bf{We can define methods within a class.}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Syntactic Sugar}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}el\_1.==(el\_2) & equivalent to: el\_1 == el\_2 \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}element.{[}{]}(num) & equivalent to: element{[}num{]} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}el.{[}{]}=(num, string) & equivalent to: el{[}num{]} = string \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Debugging with Byebug}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}require "byebug" & add to the top of your file to gain access to the gem \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}debugger & place this line at a point in your file where you want to begin debugger mode \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}l \textless{}start line\textgreater{}-\textless{}end line\textgreater{} & list the line numbers in the specified range \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} \{\{nobreak\}\}step OR s & step into the method call on the current line, once execution is paused on a line containing a method call \tn % Row Count 16 (+ 6) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}next OR n & move to the next line of executed code \tn % Row Count 18 (+ 2) % Row 5 \SetRowColor{white} \{\{nobreak\}\}break \textless{}line num\textgreater{} OR b \textless{}line num\textgreater{} & place a breakpoint at the specified line number, this will pause execution \tn % Row Count 22 (+ 4) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}continue OR c & resume normal execution of the code until a breakpoint \tn % Row Count 25 (+ 3) % Row 7 \SetRowColor{white} \{\{nobreak\}\}display \textless{}variable\textgreater{} & automatically show the current value of a variable \tn % Row Count 28 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Testing with Rspec}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}describe & name the method being tested \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}it & expresse the expected behavior of the method being tested \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}expect & show how that behavior is tested \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \{\{nobreak\}\}context & additional blocks to outline different scenarios that code is expected to satisfy \tn % Row Count 12 (+ 5) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}Class\#method & refers to the instance method in the class \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} \{\{nobreak\}\}Class.method OR Class::method & refers to the class method in the class \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{To use RSpec, we need to separate our implementation code files from the testing files using a /lib and /spec folder respectively.}} \newline \newline /example\_project \newline ├── lib \newline │   ├── add.rb \newline │   └── prime.rb \newline └── spec \newline ├── add\_spec.rb \newline └── prime\_spec.rb} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Exceptions}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}begin...rescue...end & react to an exception, the code in the begin block will execute until an exception is reached, once an exception is reached, the execution will immediately jump to rescue \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} \{\{nobreak\}\}raise & bring up an exception, flag an exceptional scenario that should be handled in a specific way \tn % Row Count 13 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}