Ruby Intro - Arrays Flashcards

To teach some intro Ruby concepts and syntax

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

What do you use to select a range of items from an array set? Difference between the two syntaxes?

A

Ranges. “..” gives entire range while “…” leaves out the last item in the range.

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

How do you specify last (or second to last, etc.) value when getting a range of elements?

A

-1 gives last value. -2 gives second to last. Can be noted like so: “3..-1”

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

What’s the syntax for an iterator to access all elements in an array? (3 lines)

A

cool_things = [“race cars”, “lasers”, “aeroplanes”]

cool_things.each do |cool_thing|
puts “I like #{cool_thing}!”
end

# prints: I like race cars!
#         I like lasers!
#         I like aeroplanes!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is &laquo_space;called when used for inserting into arrays? The _____ ________.

A

The Shovel Operator

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

Concatenate two arrays into a new one without modifying the originals?

A

new_array = array1 + array2

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

Concatenate arrays AND modify original

A

use the concat method

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

three ways to find number of elements in an array. The last way only tells True/false whether array has elements.

A

“.length” “.count” “.empty?” (empty gives True or False)

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

For arrays, what is a Stack? What methods are used to treat an array like a Stack?

A

Has LIFO features.
Using push and pop and delete.
Pop will take out an item and also delete it from the array at the same time (does not need print. Printing is just for humans!)
POP acts like DELETE! It also does not print the popped item.

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

For arrays, what is a Queue. What methods are used to treat like a Queue?

A

Has FIFO features. Using push and “delete_at(0)”

nums = []
nums << 1
nums << 2
nums << 3
nums.delete_at(0)
# => 1
nums
# => [2, 3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Array shift and unshift?

A

Shift is like “delete_at(0)” but also prints the item deleted. Unshift ADDS.

array = [1,2,3,4]
array.shift
# => 1

array
# => [2,3,4]
array.unshift(5)
# => [5,2,3,4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

in a single code line, create a sentence that expresses an array string (separated by commas) within the sentence. What is the method used to separate elements in the array?

A

“I like #{cool_things.join(“, “)}.”

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

What is the “, “ argument to join strings together called?

A

separator

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

What is method used to find out whether an array contains a specific object/element?

A

.include?( )

small_primes = [1, 2, 3, 5, 7, 11]
small_primes.include?(9)
# => false

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

How to find the position of first occurence of a specific object/element in an array?

A

.index( )

small_primes = [1, 2, 3, 5, 7, 11]
small_primes.index(3)
# => 2
small_primes.index(9)
# => nil
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Code to sort to a new array without changing the original array?

A

regular “sort”

arr = [3, 2, 1]
sorted_arr = arr.sort
sorted_arr # => [1, 2, 3]
arr # => [3, 2, 1]

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

How do you sort an existing array?

A

“.sort!”

arr = [3, 2, 1]
arr.sort!
arr # => [1, 2, 3]

17
Q

what are safe and unsafe methods?

A

unsafe methods are faster but change the original object and end with “!” Always use safe as default unless there is specific reason to use unsafe

18
Q

Method to randomly reorder elements in an array?

A

.shuffle

19
Q

Method to select/print the first or last element in an array?

A

.first or .last will select/print it

20
Q

Method to select one random element from an array?

A

.sample wil select/print it

die = [1,2,3,4,5,6]
die.sample
roll = die.sample

21
Q

Two rules for best naming arrays?

A
  1. Pluralize array name where possible
  2. Be expressive with the name instead of terse and algebraic
fancy_car_brands = ["Maserati", "Porsche", "Tesla"]
junk_models = ["Chevy Nova", "Ford Pinto"]

fancy_car_brands.each do |fancy_car_brand|
junk_models.each do |junk_model|
puts “#{fancy_car_brand} would never make junk like the #{junk_model}”
end
end

22
Q

What is a heterogenous array? Are these good or bad?

A

an array with different data types. This is generally bad. Use homogenous arrays.

23
Q

Two ways to push/shovel multiple elements into an array in a single line of code?

A

sample.push(3, 4, 5)
# or
sample &laquo_space;3 &laquo_space;4 &laquo_space;5

24
Q

What is the method that returns an array with duplicates removed? How does it work?

A

.uniq. This creates a temporary new array that has duplicates removed
bonus: .uniq! removes duplicates from original array

25
Q

What is it called when you add your new method to an Array class? How do you do this?

A

Monkey patching.
It is frowned upon. But here’s how:

class Array
  def my_uniq
    # ...
  end
end
26
Q

3 ways to create a new array of a range of numbers

A

You can create an array with a range using splat (BEST WAY),
» a=*(1..10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

using the Array constructor,
Array (1..10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

or using to_a
(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

27
Q

For arrays, how are “shift” and “delete_at(0)” different?

A

shift prints the item deleted.

delete_at(0) does not print.

28
Q

Four ways to convert string into array letters. One other way to convert to separate words array.

A

text. split(//)
text. scan(/./)
text. chars.to_a
text. each_char.to_a

text.split()

29
Q

super simple Code for converting all elements in an array into an integer/string etc.?

A

array.map(&:to_i)