Skip to content

Instantly share code, notes, and snippets.

@laurenhavertz
Last active December 18, 2015 18:19
Show Gist options
  • Save laurenhavertz/5824433 to your computer and use it in GitHub Desktop.
Save laurenhavertz/5824433 to your computer and use it in GitHub Desktop.
WDI Action Adventure

WARMUP EXERCISE:

a = [] array

b = {} hash

c[:name] hash

d[person] object

e[k] hash k = key

f[i] array i = index

index into array using index index into hash using key v = value at k = key

g.gsub(...) string

h[3] array or index number OR hash key

i.each... array or hash...depends on the context

j.slice() array but you can slice on the key or hash

m = n anything

json.parse = converts data into app applicable to read

HASHES

lookup: Keys Values Inject => enumerable

inject iterate through the values and then the end returns a value and that value should be operation of all the previous values

a = [42, 37, 17, 11, 3] a.inject (first item is cumulative result, second item is some item in the array) (first iteration: the first variable will always be nil)

a.inject { |sum, i| sum + i }

enumerable works the same on array, hash, etc.

a = ["Kristine", "Lauren", "Ian", "Brian]
a.inject do |all, one|
  all + ", #{one}"
end
=> "Kristine, Lauren, Ian Brian"`

Classes

number operates on itself

**function (pass something into function, does work, returns value)

class have a thing do work on "itself" and return value

class names should always have a capital letter "Class"

new calls initialize

w/o self = class method (calling on IDEA of "Contact" or "Person" instead of just an "instance"

.self = calling on particular "instance" not the whole idea

attr_accessor` : both attr_reader & attr_writer

# attr_reader(:name)
    def name()
        return @name
    end
    
#attr_writer(:secret)
def keep_secret=(secret)
    @secret = secret
end

to_s == inspect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment