Skip to content

Instantly share code, notes, and snippets.

@beelarr
Last active December 4, 2017 04:43
Show Gist options
  • Save beelarr/2d731814f188f3db225f4a5dffe111fc to your computer and use it in GitHub Desktop.
Save beelarr/2d731814f188f3db225f4a5dffe111fc to your computer and use it in GitHub Desktop.
// What is a class?
- They hold data
- They have methods that interact with that data
- They instantiate objects
// What is an object?
- An instance of a class
- Classes descend from the Object root class
// What is a module?
- Modules serve as a mechanism for namespaces
- Modules provide a way for multi inheritance via mixins, but can't be instantiated
// Ways to invoke a method in ruby?
1. obj = Object.new
2. obj.send :method
3. obj.method(:method).call
// ?? a ||= b
or equals
if a is false or nil it gets the value of b
// What does self mean?
class level = self refers to current object
instance level = self is the memory location
// What is unit testing?
testing methods
assert the actual expected result
// How to prevent an object from being changed
.freeze
// Advantages of Ruby
1. Pure OOL - methods, classes, and booleans are Objects
2. Open Source
3. Metaprogramming - code that acts on other code, not data.
4. Clean and Simple Syntax
//4 types of variables
1. Global - begin with $
2. Local variables - begin with lowercase letter or underscore
3. Class variables - begin with @@ shared by all instances of the class
4. Instance variables - begin with @ local, singe instance of class
//Three levels of access control
1. Public - called by anyone
2. Protected - accessible within their defining class and subclasses
3. Private - only viewed within defining class except with Object.new.send(:method) is called
//Thread lifecycle and pooling
Lifecycle - begins as soon as CPU resources are available, use up resources, but running multiple at a time can improve performance
Pooling - threads in standby, best for large number of short tasks
//Modules uses
--Modules as mixins - share methods / code among different classes
//Stabby Proc
-> (s) (p s) [“I’m a Proc”]
Create a lambda (nameless function). Takes s preforms (p s) and feeds [ ] as s.
// Fun Ruby Facts
and / or are lower than =
&& / || are higher than =
== - value
=== - used in a when clause of a case statement
eql? - value and type
equal? - identity of two objects based on object id
false and nil are the only things that are falsy
// &:
[1, 2, 3].map(&:to_s)
It takes the array of numbers, calls to_s on each item, and returns an array of the return values.
But what if you want to do the opposite? Rather than calling a method on the object, what if you want to call a method with the object as a parameter? Simple!
["1", "2", "3"].map(&method(:Integer))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment