Methods Flashcards

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

.gsub!

A

Global Substitution

example_string.gsub!(/x/, “y”)

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

.include?

A

Evaluates to true if it finds what it’s looking for and false otherwise.

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

string interpolation

A
example_string = "Andy"
print "Hello, my name is #{example_string}"
# ==> "Hello, my name is Andy"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

while

A

loop method
example:
i = 1
while i

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

until

A

loop method
a complement to the while loop method.
a condition is set in a ‘until loop’ that breaks the loop

example:
i = 0 
until i == 6
    i += 1 
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

assignment operators

A

+= , -= , *= , /= ,

example:

example_variable += 1
You’re telling Ruby: “Add 1 to ‘example_variable’, then assign that new value back to ‘example_variable’.” This provides a succinct way of updating variable values in our programs.

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

for

A

loop method for when you do know how many times you’ll be looping

example:

for example_variable in 1…10

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

Inclusive and Exclusive Ranges

A

You saw a bit of new syntax in the previous exercise:

for num in 1…10

What this says to Ruby is: “For the variable num in the range 1 to 10, do the following.” The following was to print “#{num}”, so as num took on the values of 1 to 9, one at a time, those values were printed to the console.

The reason Ruby counted to 9 and not 10 was because we used three dots in the range; this tells Ruby to exclude the final number in the count: for num in 1…10 means “go up to but don’t include 10.” If we use two dots, this tells Ruby to include the highest number in the range.

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

iterator

A

A Ruby method that repeatedly invokes a block of code. The code block is just the bit that contains the instructions to be repeated.
The simplest iterator is the ‘loop’ method.

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

{}

A

do (to open the block)

example:
i = 0
loop do
  i += 1
  print "#{i}"
  break if i > 5
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

end

A

to close the block

example:
i = 0
loop do
  i += 1
  print "#{i}"
  break if i > 5
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

break

A

breaks a loop as soon as its condition is met.

example:
i = 0
loop do
  i += 1
  print "#{i}"
  break if i > 5
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

next

A

can be used to skip over certain steps in the loop.

example:
for i in 1..5
  next if i % 2 == 0
  print i
end
  1. In the above example, we loop through the range of 1 through 5, assigning each number to i in turn.
  2. If the remainder of i / 2 is zero, we go to the next iteration of the loop.
  3. Then we print the value of i. This line only prints out 1, 3, and 5 because of the previous line.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

array

A

a method where we can store multiple numbers or objects in a variable.

example:
my_array = [1, 2, 3, 4, 5]

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

.each

A

A method which can apply an expression to each element of an object, one at a time. The syntax looks like this:

object. each do |item| # Do something
- The variable name between | | can be anything you like: it’s just a placeholder for each element of the object you’re using .each on.

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

.times

A

a method thats like a super compact ‘for loop’: it can perform a task on each item in an object a specified number of times. The syntax looks like this:

10.times { print “Chunky bacon!” }

17
Q

.split

A

a method to divide the user’s input into individual words.

it takes in a string and returns an array.

18
Q

multidimensional array

A

array of an array

example:
multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

19
Q

hash

A

a collection of key-value pairs.

example:

hash = {
  key1 => value1,
  key2 => value2,
  key3 => value3
}

Values are assigned to keys using =>. You can use any Ruby object for a key or value.

20
Q

literal notation hash

A
literally describes what you want in the hash: you give it a name and you set it equal to a bunch of key => value pairs inside curly braces.
my_hash = { "name" => "Eric",
  "age" => 26,
  "hungry?" => true
}

puts my_hash[“name”]
puts my_hash[“age”]
puts my_hash[“hungry?”]

21
Q

hash.new

A

Setting a variable equal to Hash.new creates a new, empty hash; it’s the same as setting the variable equal to empty curly braces ({}).

example:

my_hash = Hash.new

22
Q

splat arguments

A

arguments preceded by a *, which signals to Ruby: “Hey Ruby, I don’t know how many arguments there are about to be, but it could be more than one.”

example:

def what_up(greeting, *bros)

23
Q

return

A

Sometimes we don’t just want a method to print something to the console, but we actually want that method to hand us (or another method!) back a value.

example:

24
Q

blocks

A

You can think of blocks as a way of creating methods that don’t have a name.
Blocks can be defined with either the keywords do and end or with curly braces ({}).

25
Q

Abstraction / Abstracting

A

an important idea in computer science, and you can think of it as meaning “making something simpler.”

26
Q

.sort!

A

.sort! sorts an array in-place (that is, it destroys the original array and replaces it with the sorted version).

27
Q

combined comparison operator

A

It returns 0 if the first operand (item to be compared) equals the second, 1 if first operand is greater than the second, and -1 if the first operand is less than the second.

28
Q

.sort

A

returns a sorted array while leaving the original array alone.

29
Q

.to_sym

A

converts strings to symbols
example:

“example”
“example”.to_sym => :example

30
Q

.intern

A

to internalize.

another way to convert strings to symbols

31
Q

.select

A

to filter a hash for values that meet a certain criteria