Skip to content

Instantly share code, notes, and snippets.

@Alacrity01
Created April 24, 2019 19:06
Show Gist options
  • Save Alacrity01/f59289677f0a65202c10694f097e2055 to your computer and use it in GitHub Desktop.
Save Alacrity01/f59289677f0a65202c10694f097e2055 to your computer and use it in GitHub Desktop.
Week 1, Day 3
Week 1, Day 3
Reader methods are used to access instance variables.
Instance variables are contained to a particular instance of an object (e.g. *that* chair; not chairs in general).
Objects are combinations of attributes and behaviors. Behaviors in Ruby are methods.
Reader methods (getter methods); writer methods (setter methods).
r + tab in sublime will add the attr_reader method (e.g. attr_reader :first_name)
w + tab adds attr_writer method (e.g. attr_writer :active)
attr_reader :first_name
Replaces:
def first_name
@first_name
end
attr_writer :active
Replaces:
def active=(new_value)
@active = new_value
end
**Highlight a repeated phrase and hit control + d to edit with multiple cursors
# {} not required when hash is used for creating a new instance of an object; Ruby recognizes it as a hash because it is very common
e.g.
employee_1 = Employee.new(first_name: "Helena", last_name: "Bonom Carter", salary: 80000, active: true)
Inheritance:
class Manager < Employee # means that Manager inherits all functionality of Employee because Manager is a type of Employee
super runs a method from the Super class when you run it inside the subclass.
• option + 8 creates a bullet point ••••••••••••••••••••••••••
Instance variable vs. class variable: @ is used for instance, @@ used for class.
Example--
@id represents an instance variable of id
@@count represents a class variable
You would use a class method when calling the method as an instance method would change the number of objects and you don't want to do that.
Example--
class Sheep
(see sheep.rb from Slack)
Sheep.new creates a new instance of Sheep
p Sheep.how_many
private keyword will make all methods after within the class hidden.
•It's used not for security but more as a guide to someone else for how the code is intended to be used.
Scope--
local_variable = snake_case Not available outside of the place in which it was created (inside or outside of a method)
@instance_variable = snake_case with @ symbol at beginning
@@class_variabe = snake_case with 2 @ symbols at beginning
CONSTANT = All uppercase, should define once while program is running and never change.
Anything that starts with a capital letter becomes a constant in Ruby. (Classes are technically constants)
ClassName = UpperCamelCase, special type of constant.
••• Limiting the scope of your variables is essential. The list above goes from smallest to largest. •••
Instance methods are also snake cased. Class methods are snake case but have self. in front of them.
? at the end of a method makes it boolean (it is intended to return as true or false)
! <-- called a bang; means it is destructive (it changes the object you are calling it on)
p array.compact <-- will print the object array with all nil values removed, but will not change the object array
p array.compact! <-- works the same as compact except changes array; printing array on subsequent
lines will still have nil values removed
x = 5 <-- assigns a value of 5 to x
x == 5 <-- compares the value of x to 5
x != 5 <-- checks to see if x does not equal 5
x < 5 <-- comparison operator to see if x is less than y
x <=> y <-- "spaceship operator"...doesn't come up often except when sorting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment