Ruby raw log

· nulo's blog

#check #lingo #quirk #style #links #core #docs #stdlib #split #sort_by #reverse #each #to_sym #intern #select #methods #upto #downto #array #sage #regex #method #rubydocs #variables #general #odd #prepend #unshift

#check - clarify further and remove this
#lingo - precise technical terms
#quirk - ruby specific shenanigans
#style - ruby style conventions
#links - self explanatory

#core #docs https://ruby-doc.org/core-2.7.5/
#stdlib #docs https://ruby-doc.org/stdlib-2.7.5/

# Codecademy Ruby Track - Lessons 1 thru 8

==
!=
<
<=
>
>=

#lingo

#quirk

# Intro Ruby Book notes

#links https://rubystyle.guide/ #style

# loops and iterators

#quirk unlike the loop method, while is not implemented as a method. One consequence of this difference is, that unlike loop, a while loop does not have its own scope -- the entire body of the loop is in the same scope as the code that contains the while loop.

#quirk A range is a special type in Ruby that captures a range of elements. For example 1..3 is a range that captures the integers 1, 2, and 3.

#quirk The odd thing about the for loop is that the loop returns the collection of elements after it executes, whereas the earlier while loop examples return nil

loop is special in that it is a method and defines a block parameter with its own block scope

Iterators are methods that naturally loop over a given set of data and allow you to operate on each element in the collection.

# Arrays

<< - shovel operator pop, push and << mutate the caller

map return a new transformed array delete_at takes an index and deletes the element at that index - mutating delete deletes all instances of the given value from the array - mutating returns the deleted elements if the elements existed returns nil otherwise uniq iterates through an array an deletes all duplicates and returns a new resulting array uniq! is the mutating counterpart array method to uniq select iterates over the array and returns a new array with elements that return true for the given expression

It is important to know what a method is doing to its arguments and what it is returning.

unshift is like the opposite of push and returns the resulting mutated array - mutating to_s returns a string version of the calling array

#docs https://ruby-doc.org/core-3.1.2/Array.html #array

#methods include? is a predicate that tests if a given object exists in the array flatten returns a new flattened array each_index iterates over the indexes of the array(not values) returns the original/calling array each_with_index iterates over the array while providing the val, idx (in that order)of each element returns the calling array sort returns a new array with elements in sorted order product returns a new nested array that contains the combination of all elements from all arrays each works on objects that allow for iteration, takes a block and runs the code in the block once for each element returns the collection it was invoked on map also works on objects that allow for iteration, takes a block and runs the code in the block once for each element returns a new array containing the values retuned by the block

Array#[] - this is an array method a.k.a Array Element Reference Array#[]= - this is an array method a.k.a Array ELement Assignment

# Hashes

A hash is a datastructure that stores items by associated keys.

#methods merge - merges 2 hashes together (if a key exists in both hashes, the value from the argument wins) returns the merged hash merge! - mutating version of merge

#quirk For a method invocation, the curly braces, { }, are not required when a hash is the last argument.

#sage It's impossible to know everything in the beginning so put some effort into learning a few things well and then build from there.

While choosing between a hash or an array, think of: - does the data have a natural label or need a label - hash suits better - is the data ordered - array suits better - is a stack or queue structure needed - array suits better

#quirk keys in a hash can be of any type - symbols are the default type for them when using non-symbol keys, hash-rocket syntax is necessary

#methods key? - predicate to test the existence of a specific key in a hash select - takes a block and iterates over the hash passing each kv pair as an argument to it returns a new hash with kv pairs from calling hash that returned true when passed to the block fetch - takes a key and returns the associated value, can also specify a default value as a second argument if the key does not exist to_a - returns an array version of the calling hash keys - returns an arrays of all keys values - returns an array of all values each_key - iterates over the hash and takes a block passing each key as an argument to the block returns the calling hash each_value - calls block once for each value in the calling hash passing the value as an argument returns the calling hash each - calls block once for each kv pair passing both key and value as arguments returns the calling hash

#quirk Starting Ruby 1.9 hashes maintain order

# More stuff

#regex =~ is the regex operator, it compares a string to a given pattern and returns the index of the character in the string if found, nil otherwise this return value can be used as a boolean as the index evaluates as true eg: string = /b/

#method match can also be used for regex comparisions returns a MatchData object if there is a match, nil otherwise eg: /b/.match(string)

#rubydocs #quirk or has a super low precedecne even below = or (and similarly and) is best used not for combining boolean expressions, but for control flow, like in

do_something or raise "some error!"

where do_something returns false or nil when an error occurs.

#variables If you call a method that mutates the caller, it will change the value in that object's address space, and any other variables that also point to that object will be affected. #check how this relates to mutability

&identifier pattern is used to denote a block parameter in a method definition eg: def take_block(&block); end

#quirk When Ruby encounters a def statement, it merely reads the function definition into memory and saves it away to be executed later. The body of the method isn't executed until we actually call the method.

#lingo =~ - regex match operator which returns an integer when there is a match and nil otherwise.

#array Array() constructor takes only one argument

#general in a single line if statement we are "using if as a staement modifier"

#array #methods Array#prepend is an alias to Array#unshift - mutating