Common Ruby Methods Flashcards

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

Array.each

A

Iterates over each element in the array

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

Array.map or collect

A

Transforms each element of the array based on the provided block and returns a new array with the transformed elements.

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

Array.select or find_all

A

Returns a new array containing all elements for which the block returns true.

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

Array.reject

A

Returns a new array containing all elements for which the block returns false.

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

Array.find or detect

A

Returns the first element for which the block returns true.

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

Array.include?

A

Checks if the specified element exists in the array.

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

Array.first

A

Returns the first element of the array

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

Array.last

A

Returns the last element of the array

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

Array.empty?

A

Checks if the array is empty

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

Array.size or length

A

Returns the number of elements in the array.

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

Array.push or «

A

Adds one or more elements to the end of the array

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

Array.pop

A

Removes and returns the last element of the array.

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

Array.unshift

A

Adds one or more elements to the beginning of the array.

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

Array.shift

A

Removes and returns the first element of the array.

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

Array.concat

A

Concatenates one array onto another

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

Array.join

A

Combines all elements of the array into a single string.

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

Array.sort

A

Sorts the elements of the array.

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

Array.reverse

A

Reverses the order of the elements in the array.

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

Array.uniq

A

Returns a new array with duplicate elements removed.

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

Array.compact

A

Returns a new array with nil elements removed.

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

Array.filter_map (2.7+)

A

Returns a new array containing the results of running the block once for every element in the enum, only including the results for which the block returns a truthy value.

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

Array.inject or reduce

A

This method is used to combine all elements of a collection (such as an array or a hash) by applying a binary operation, specified by a block or a symbol, repeatedly to each element in the collection.

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

Hash.fetch(key)

A

Retrieve the value associated with a given key, or return a default value if the key is not found.

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

Hash.store(key, value)

A

Store a value with a given key.

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

Hash.merge!(other_hash)

A

Merge another hash into the current hash, modifying it in place.

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

Hash.delete(key)

A

Remove a key-value pair from the hash by key.

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

Hash.clear

A

Remove all key-value pairs from the hash.

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

Hash.each

A

Iterate over each key-value pair in the hash.

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

Hash.each_key

A

Iterate over each key in the hash.

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

Hash.each_value

A

Iterate over each value in the hash.

31
Q

Hash.empty?

A

Check if the hash is empty.

32
Q

Hash.size or length

A

Return the number of key-value pairs in the hash.

33
Q

Hash.keys

A

Return an array containing all keys in the hash.

34
Q

Hash.values

A

Return an array containing all values in the hash.

35
Q

Hash.has_key?(key) or key?(key)

A

Check if the hash contains a specific key.

36
Q

Hash.has_value?(value)

A

Check if the hash contains a specific value.

37
Q

Hash.transform_keys

A

Return a new hash with keys transformed by the given block.

38
Q

Hash.transform_values

A

Return a new hash with values transformed by the given block.

39
Q

Hash.invert

A

Return a new hash with keys and values swapped.

40
Q

Hash.merge(other_hash)

A

Merge another hash into the current hash, returning a new hash.

41
Q

Hash.update(other_hash)

A

Merge another hash into the current hash, modifying it in place.

42
Q

Hash[“value”]

A

Retrieve the value associated with a given key

43
Q

String +

A

Concatenates two strings.

44
Q

String «

A

Appends the contents of the given string to the original string.

45
Q

{String}

A

Allows you to embed Ruby expressions within strings.

46
Q

String.length or .size

A

Returns the length of the string.

47
Q

String[i]

A

Returns the character at the specified index.

48
Q

String.slice

A

Returns the substring starting at the given index.

49
Q

String.upcase

A

Converts the string to uppercase.

50
Q

String.downcase

A

Converts the string to lowercase.

51
Q

String.capitalize

A

Converts the first character to uppercase.

52
Q

String.swapcase

A

Swaps the case of each character.

53
Q

String == String

A

Checks if two strings are equal.

54
Q

String.casecmp

A

Performs a case-insensitive comparison.

55
Q

String.include?

A

Checks if the string contains a substring.

56
Q

String.index

A

Returns the index of the first occurrence of a substring.

57
Q

String.scan

A

Returns an array of all occurrences of a pattern.

58
Q

String.slice

A

Returns a substring based on the given index or range.

59
Q

String.split

A

Splits the string into an array of substrings based on a delimiter.

60
Q

String.partition

A

Splits the string into three parts based on a delimiter.

61
Q

String.gsub

A

Replaces all occurrences of a pattern with a specified string.

62
Q

String.sub

A

Replaces the first occurrence of a pattern with a specified string.

63
Q

String.insert

A

Inserts a substring at a specified index.

64
Q

String.strip

A

Removes leading and trailing whitespace.

65
Q

String.lstrip

A

Removes leading whitespace.

66
Q

String.rstrip

A

Removes trailing whitespace.

67
Q

String.center

A

Centers the string within a specified width.

68
Q

String.ljust

A

Left-justifies the string within a specified width.

69
Q

String.rjust

A

Right-justifies the string within a specified width.

70
Q

String.to_i

A

Converts the string to an integer.

71
Q

String.to_f

A

Converts the string to a floating-point number.

72
Q

String.to_sym

A

Converts the string to a symbol.

73
Q

String,encode

A

Changes the encoding of the string.

74
Q
A