Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 1.55 KB

ruby-cheat-sheet.md

File metadata and controls

38 lines (30 loc) · 1.55 KB

Ruby: Overview

Ruby is an object-oriented programming language that was created in 1993 by Yukihiro Matsumoto.

Classes

A Ruby class is the blueprint from which individual objects are created.

Comments

Comments are denoted by using a hash, or '#' before the text

Data Types

Types of data used in the language include numbers, strings, ranges, arrays, and hashes.

  • Array - list of elements in an order.
  • Hashes - a collection of key-value pairs.
  • Ranges - (0..9 would output 0 to 9) return a range of numbers or letters.
  • String literals - created using the double quote character, ".
  • Strings - string of characters sent from the server to the browser.

Methods

Ruby has several built-in methods. Some common ones include:

  • .first, .second, .last - these are used to access elements.
  • .length - this returns the length of a string/array.
  • .empty? - used to determine whether or not a data field is empty.
  • .include? - if a datatype includes anything in particular.
  • .sort, .reverse, .shuffle - these are useful for organizing the data in arrays.

Operators

  • Bang operator (!) can be used to mutate an array.
  • Shovel operator (<<) used to add to arrays.

IRb

Interactive Ruby (IRb) is a shell used for experimenting with Ruby. It's a console that you can use for the language in your terminal.

Symbols

A symbol is an object type that is often used in hashes. They are denoted with a colon, like :description. The following is an example of how they would appear in a hash:

person = {:name =>"Sarah", :id =>20, :birthday_month =>"October"}