OverAPI.com | Collecting all the cheat sheets
Ruby QuickRef
Table of Contents Language General Tips These are tips I’ve given over and over and over and over… Use 2 space indent, no tabs. See for more. General Syntax Rules Comments start with a pound/sharp (#) character and go to EOL. Reserved Words alias and BEGIN begin break case class def defined? Types Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes. Numbers 1231_234123.451.2e-30xffff 0b01011 0377 ? Strings In all of the %() cases below, you may use any matching characters or any single character for delimiters. %[], %!! 'no interpolation'"#{interpolation}, and backslashes\n"%q(no interpolation)%Q(interpolation and backslashes)%(interpolation and backslashes)`echo command interpretation with interpolation and backslashes`%x(echo command interpretation with interpolation and backslashes) Backslashes: Here Docs: Encodings: Waaaay too much to cover here. Symbols Internalized String. Ranges 1..101...10'a'..' Regexen "r"
Ruby Loops - while, for, until, break, redo and retry
Loops in Ruby are used to execute the same block of code a specified number of times. This chapter details all the loop statements supported by Ruby. Ruby while Statement: Syntax: while conditional [do] code end Executes code while conditional is true. Example: #! This will produce the following result: Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4 Ruby while modifier: code while condition OR begin code end while conditional Executes code while conditional is true. If a while modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated. #! Ruby until Statement: until conditional [do] code end Executes code while conditional is false. #! Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5 Ruby until modifier: code until conditional OR begin code end until conditional Executes code while conditional is false. #! #!
Ruby Tutorial : Arrays
Ref. WGR Chapter 9, Section 9.2, Collection handling with arrays Array sized dynamicallycan contain mixed typeszero-indexedcan be defined literally (inline) e.g. @@@ ruby fruits = ["apple", "banana"] @@@ ruby a = [1, 2, 3] a.push "four" #=> [1, 2, 3, "four"] a.pop #=> "four" a #=> [1, 2, 3] a[0] #=> 1 Adding to an array @@@ ruby a = [] a << "x" #=> ["x"] << is the "shovel" operator More ways to add to an array @@@ ruby a = ["x"] a.push "y" #=> ["x", "y"] a + "z" #=> can't convert String into Array a + ["z"] #=> ["x", "y", "z"] (but a is unchanged) a += ["z"] #=> ["x", "y", "z"] a.concat ["w"] #=> ["x", "y", "z", "w"] concat and + make a copypush and += are destructive Q: Why doesn't push end in a bang? size isn't everything size and length are synonymsboth give you the number of items in the array Accessing array items Arrays are zero-indexed @@@ ruby fruit = ["apple", "banana", "cherry"] fruit[0] #=> "apple" fruit[2] #=> "cherry" fruit[3] #=> nil Zero Is Better Than One Fun with Array Indexes