
I need to brush up on my Ruby and Rails skills and decided to write about it. I found Launch School and their open book self to be a great resource. In my previous articles I talked about setting up Ruby and Rails on Ubuntu, explained the MVC architecture, how a rails application is structured and the basics of Ruby. In this article I am going to continue with the basics of Ruby, after reading this article you will understand:
If you missed the previous article check it out!!!!!
- How Loops & Iterators work in Ruby?
- What are Arrays in Ruby?
- What are Hashes in Ruby?
- How do variables as pointers work?
- What are blocks and procs
- Exception Handling
What is a Loop in Ruby?
A loop will execute any code within a block until you manually intervene with Ctrl + c or inserting a break statement inside the block.
What is a while loop in Ruby?
A while loop is given a parameter that evaluates to a boolean, once the boolean expression becomes false the while loop will not execute again, program continues to the next operation.
What is a until loop in Ruby?
The until loop is simply the opposite of the while loop.
What is a Do/While loop in Ruby?
The Do/While loop is similar to the while loop.
What is a Iterators in Ruby?
Are methods that naturally loop over a given set of data and allow you to operate on each element in the collection. You can call the each method on an array to loop through each element and print it to the screen, each time you loop over an element in an array you need to save that information to a variable and then use puts to print the element to the screen.
What are Arrays in Ruby?
An array is an ordered list of elements that can be any type. The reason it is referred to as an ordered list is because array’s store items by an ordered index.
How do you modify an array?
I guess this would be a good time to check my learning because mutating the caller would come into play because you could modify an array by using the pop method inside a loop and change the array outside the method definition. But you could also change the array by using the pop method. The pop method takes the last element and returns it.
You can also use the push method which will add an element to the back of the array.
What does the map method do?
Iterates over an array applying a block to each element of the array and returns a new array with those results. The collect method is an alias to the map method they do the same thing. Map and collect are not destructive, they DO NOT MUTATE THE CALLER.
What does delete_at do?
Can be used to eliminate a value at a certain index from an array. Be careful because it will modify your array destructively. ONCE YOU CALL THIS METHOD, YOU ARE CHANGING THE ARRAY PERMANENTLY.
What does uniq do?
It is not destructive, it loops through the array and deletes any duplicate values that exist, then returns the result as a new array. It does not modify the original array but returns a new array with the duplicates removed. Use unip! to be destructive.
How do you iterate over an array?
You can use select, to select a certain number of element, does not mutate the caller. Below example is from
What methods do Array has access too?
include?: checks to see if the arguments are included in the array
flatten: creates a one-dimensional array, is it destructive
each_index: passes the index of each element into a block, basically prints the index value of each element instead of the value. OG array returned.
each_with_index: passes both value and index into a block, the first variable stores the value and the second variable stores the index.
sort: sorts an array
What is the difference between map and each?
Use each for iteration and map for transformation
What are Hashes in Ruby?
Is a key-value pair sometimes referred to as a dictionary, a hash uses symbols as keys and any data types are values. All key-value pairs in a hash are surrounded by curly braces {} and comma separated. Hashes can be iterated over and have optional parameters. Hash keys can be something another then a symbol, you could represent a hash key by using a different data type such as a string, array, integer or float. If you deviate from the symbol way you must use the => instead.
What is the difference between Hashes and Arrays?
If you order matters or you need to mimic a stack or queue use an ARRAY. If need you associate data to a label use a HASH.
What are some hash methods?
has_key?: checks if a hash contains a specific key
select: You can pass a block and will return any key-value pairs that evaluate to true.
fetch: pass a key to returns the value if the key exists.
to_a: return an array version of hash
keys: retrieves all the keys
values: retrieves all the values
How variables as pointers work?
Variables are pointers to physical space in memory, variables are labels programmers create to refer to some physical memory address. I will explain this more in a future article.
What are Block and Procs?
Blocks: can be passed into a method just like normal variables, if you block a you must use the &. The block must always be the last parameter in the method definition. I will explain blocks in more detail I think they need their own article because they are very powerful.
Procs: are blocks that are wrapped in a proc object and stored in a variable to be passed around.I will explain blocks in more detail in a future article.
What is exception handling?
Is a specific process that deals with errors in a manageable and predictable way. Ruby has a exception class that makes handling these errors much easier. You would use exception handling for anything that can behave in an unpredictable manner. nil value
begin: perform some dangerous operation
rescue: do this if operation fails, i.e log the error
I will explain testing in more detail in a future article, thanks again for reading and go check out the Open book shelf at Launch School. In the next article I will go over Object Oriented Programming with Ruby.