Ruby Methods Cheat Sheet Flashcards

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

Find the characters in specific positions in a STRING.

A

“string”[1..3]

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

Find the elements in specific positions in an ARRAY.

A

array[1..2]

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

Find the value of specific key in a HASH.

A

hash[“key”]

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

Find first or last value(s) in a STRING.

A

“string”[0]
[-1]
[-3…-1]

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

Find first or last value(s) in an ARRAY (3 ways)

A

.first / .last
.first(n) / .last(n)
Array[0] or [-1]

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

Find first or last value(s) in a HASH

A

Not necessary. Can do for multl-dimensional hash, but that’s too advanced for now. Ignore.

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

Find the positions of specific characters in a STRING.

What is returned?

A

“string”.index(“s”)

Returns the first location

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

Find the positions of specific elements in an ARRAY.
What is returned?
How do you find elements that fulfills a specific criteria test?

A

array.index(“element1”)
Returns the first location.

With a block:
array.index {|x| x == “apple”}

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

Find the key of a certain value or the value of a certain key in a HASH.

A

hash. key(“xxx”) gets the key

hash. value(“xxx”) gets the value

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

Get all the keys or values of a HASH

A

hash. keys

hash. values

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

All the ways to get number of all characters in a STRING

A

2
.length
.size
** ONLY exception to remember is that you can’t COUNT STRINGS.

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

All the ways to get number of all elements in an ARRAY

A

3
.length
.size
.count

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

All the ways to get number of all key-value pairs in a HASH

A

3
.length
.size
.count.

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

Get the total number of all elements that pass a certain test in an ARRAY

A

.count with a block:

array.count {|x| x > 2}

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

Get the total number of all key-value pairs that pass a certain test in a HASH

A

.count with a block:

array.count {|key, value| key > 2}

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

Convert a STRING into an array of words

A

.split()

17
Q

Convert a STRING into an array of characters (all methods)

A

6

.split(//)
.split("")
.scan(/./)
.chars
.chars.to_a
.each_char.to_a
18
Q

Convert ARRAY into a string

A

.join()

.join(“, “) to add a separator between elements

19
Q

Remove returns or trailing spaces from a STRING

A

.chomp / .chomp!

.strip / .strip!

20
Q

Flatten multi-dimensional ARRAYS

A

.flatten / .flatten!

.flatten(1) flattens just one level

21
Q

Find and replace something in a STRING

A

.gsub / .gsub!

gsub(pattern, hash)

22
Q

All methods to add to STRING (with modifying original rules)

A

4

+ doesn’t modify original
&laquo_space; modifies
+= modifies
concat() modifies

23
Q

All methods to add to ARRAY (with modifying original rules)

A

6

\+    doesn't modify original
<<  modifies
\+=  modifies
.concat()   modifies
.push()      modifies
.unshift()   modifies
24
Q

All methods to add to HASH (with modifying original rules)

A

.merge / .merge!

25
Q

All methods to remove from STRING

A

.delete(“a”) deletes characters “a”

26
Q

All methods to remove from ARRAY

A

6

.delete(“apple”) - removes all “apple”s
.delete_at(2) - at index
.delete_if {block} - conditional
.pop - at last position
.shift - at first position
.keep_if {block} - conditional

27
Q

All methods to remove from HASH

A

3

.delete(“a”) removes pair of key “a”
.delete_if {block}
.keep_if {block}

28
Q

Boolean test to see if STRING contains a character(s)

A

.include?(“y”)

To match patterns use Regex / / as in
/hay/.match(“haystack”)

29
Q

Boolean test to see if ARRAY contains any element(s)

A

.include?(“apple”)

30
Q

Boolean test to see if HASH contains any pair(s) with key or value

A

.has_key?(“a”)

.has_value?(100)