Instance Methods Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

clear

A

Fn: Removes all key-value pairs from the hash
Returns: original hash
Syntax: my_hash.clear

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

invert

A

Fn: switch around the keys with the values
Returns: new hash
Syntax: my_hash.invert

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
#keys
#values
A

Fn: create a new array with the keys/values of the hash as elements
Returns: a new array
Syntax: my_hash.keys(values)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

values_at

A

Fn: returns the values associated with the given keys
Returns: an array of the values
Syntax: my_hash.values_at(key…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

update

A

Fn: Add the contents of another hash (overwriting with values from the new hash unless a block is specified)
Returns: original hash
Syntax: my_hash.update(new_hash)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
#merge
#merge!
A

Fn: Returns a hash containing values from both
Returns: a new hash/original hash (!)
Syntax: same as #update

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

has_key? / #key? / #member

A

Fn: returns true if the given key is present in hash
Returns: Boolean
Syntax: my_hash.hash_key(key)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

has_value? / #value?

A

Fn: returns true if the hash contains the given value
Returns: Boolean
Syntax: same as #key?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
#each_key
#each_value
A

Fn: Calls the block once for each key, passing the key/value as a parameter
Returns: original hash
Syntax: my_hash.each_key{|key| block}
my_hash.each_value{|value| block}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

each_pair

A

Fn: Calls the block once for each key in the hash, passing both key and value as parameters.
Returns: original hash
Syntax: my_hash.each_pair{|key, value| block}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

eql?

A

Fn: returns true if both arguments are hashes with the same content
Returns: Boolean
Syntax: my_hash.eql?(other_hash)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

to_s

A

Returns a string representation of the hash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

length

A

Returns the number of key-value pairs in the hash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
#delete_if
#keep_if
A

Like the array versions, only there are parameters in the block for both key and value rather than just index.
Destructive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

flatten

A

Fn: Returns an array that is a one-dimensional flattening of the hash. Does not flatten recursively, just to the given level.
Returns: an array
Syntax: my_hash.flatten(level)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
#empty?
#include?
A

Like the array versions in that it returns true if the hash is empty or if the given object is included (as a key)

17
Q
#any?
#all?
A

Fn: Returns true if the given block is truthy for any or all elements.
Returns: Boolean
Syntax: my_hash.any?{|key, value| block}

18
Q
#each / #each_pair
#each_with_index
A

Fn: Calls the block once for each key in hash, passing the key and value (each/each_pair) or the key and index (hashes do have indices!)
Returns: original hash
Syntax: my_hash.each{|key, value(or index)| block}

19
Q
#select
#filter
A

Fn: returns an array containing elements for which the block returns true
Returns: new_ary
Syntax: my_hash.select{|key, value| block}

20
Q

map

A

Fn: returns a new array with the results of running the block for each element in the hash
Returns: new_ary
Syntax: my_hash.map{|key, value| block}

21
Q

each_with_object

A

Fn: iterates the block for each element, passing an arbitrary object which is then returned from the method.
Returns: object
Syntax: each_with_object(obj) { |(*args), memo_obj| … } → obj

22
Q

partition

A

Fn: Split elements into those that are truthy or falsely for a given block
Returns: an array of two arrays - the first containing the elements for which the block returned true, the second for those where it returned false
Syntax: my_hash.partition{|obj| block}

23
Q
#reject
#reject!
A

Fn: returns a hash with all the elements of the original hash for which the block returns false
Returns: new hash or original (!) (or nil if no changes were made with !)
Syntax: my_hash.reject{|key, value| block}

24
Q

replace

A

Fn: Replaces the contents of one hash with another’s
Returns: original hash
Syntax: my_hash.replace(other_hash)