Intro to Ruby & Web Dev: Lesson 1
01 Sep 2014Items covered:
- Hashes vs. Arrays
- Debugging
- Pass by Reference vs. Pass by Value
- Enumerables
Hashes vs. Arrays
Debugging
How to debug? - Tips on how to work through bugs in your code or large problems where you don't know where to start.
- Google - copy and paste error into Google. Someone has had your issue before.
- Write out pseudocode - write what you are attempting to do in code in plain engish
- Break problems into small, numerical steps - start with what looks familiar ( or you know how to do)
- Ask questions in the forum (Tealeaf)
- Use binding.pry
- Paste Errors or problems in Gist - take issue to IRC chat
- Rubberduck debugging. When you come across a problem - explain how to came to that problem to your (fictitious) coworker - Ms. Rubberduck. Even better, explain the problem to someone who has no programming experience at all.
- Step away from the problem.
Pass by Value va. Pass by Reference
Some methods mutuate the caller, while others do not. To better understand this fact, let's look at: Pass by Value vs Pass by Reference: Pass by Value - when a method is called on an object, the variable is just representation of the value and not the actual object (the reference), itself. When called, the method can never change the object itself. To effect change to the object, you must reassign the variable.
In this case below, outter_str remains unchanged as a result of the method being called.
def some_method(str)
str.changeit
end
outter_str = "hi"
some_method(outter_str)
Pass by Reference - When a method is called, the object acts a pointer and a modification is actually made to an actual space in memory.
In the case below, outter_str is just a reference (pointer) to str, therefore any mutations to str will be reflected in the value of str
def some_method(str)
str.changeit
end
outter_str = "hi"
some_method(outter_str)
Ruby is Passing by the Reference of the Caller: Whether or not the method creates a pass by reference or pass by reference scenario depends on whether the method mutates the caller or not
Enumerables
Tags: tealeaf