Ruby: Block basics
A Block in Ruby is equivalent to a Lambda or Anonymous (un-named) Function in other languages. It starts with do, ends with end, and its parameters are defined between |…
Ruby: Array basics
Mixed Types In Ruby, you don't have to make all of the elements of an Array be the same type: mixed_array = [1, "two", 3.0, joe] Indices Arrays are 0-indexed,…
Ruby: Array: uniq
uniq is a method of an Array that returns the unique values of the Array
Ruby: Ranges
Using 2 dots (1..5) in the Range means 1 - 5 inclusive (1, 2, 3, 4, 5) Using 3 dots (1...5) in the Range is exclusive; i.e. it excludes the…
Ruby: Comparisons, ==, ===
=== is the case-equal operator Comparing numbers pry> items = 1 pry> items === 1 => true pry> items === 2 => false Comparing ranges A range is case-equal with…
Ruby: try, catch, finally, throw, rescue, raise
begin-rescue-end Instead of a try-catch statement from other languages, Ruby uses begin-rescue-end. The rescue block (aka catch) is executed when an exception is thrown from the begin section: begin compute_median(scores)…
Ruby: Constants
LIMIT = 100 Even though LIMIT is a constant, Ruby will still allow you to change its value. It just gives you a warning when you do it, not an…
Ruby: Case (aka Switch) Statements
Case Statements Switch statements are formed using the case keyword in Ruby. Consider the following if - elsif - if statement: # statement-1 if item_counts[item_id] === 1 count = 1…
Ruby: snake_case, camelCase, or PascalCase?
For Ruby, use snake_case for variables, functions, and methods: product_score = 8