Control Flow Flashcards

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

true && true # => ___
true && false # => ___
false && true # => ___
false && false # => ___

A

true
false
false
false

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

true || true # => ___
true || false # => ___
false || true # => ___
false || false # => ___

A

true
true
true
false

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

false || false

true or false?

A

false

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

!true # => ____

!false # => ____

A

false

true

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

!true && !true

true or false?

A

false

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

!true && (!true || 100 != 5**2)

true or false?

A

false

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

true || !(true || false)

true or false?

A

true

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

Set the output to write “Ruby!” five times.

A

5.times { puts “Ruby!” }

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

Write an array of numbers 1-5. Note the brackets.

A

numbers = [1, 2, 3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Write a hash “profile” with your name, city, and state.
  2. Call on the city within that profile.
  3. Create a new hash called “favorites”.
  4. Add a value pair where “baseball” = “Twins”.
A
profile = {
  "name" => "Spencer"
  "city" => "Minneapolis"
  "state" => "MN"
}

puts profile[“city”]

favorites = Hash.new
favorites["baseball"] = "Twins"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

example = [“Car”, “Boar”, 45, 9.9, true]

Access “45” in the array.

A

puts example[2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Create an array “team” of these players and their number:
    Towns = 32
    Russell = 0
    Beasley = 5
  2. Call on the array to form a statement of what each number every player wears.
A
team = {
  "Towns" => "32",
  "Russell" => "0",
  "Beasley" => "5",
}

team.each do |player, number|
puts “#{player} wears #{number}.”
end

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

Define a greeter method that takes a single string parameter, name, and returns a string greeting that person.

A
def greeter(name)
  return "Hello, #{name}!"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define a by_three? method that takes a single integer parameter, number, and returns true if that number is evenly divisible by three and false if not.

A
def by_three?(num)
  if num % 3 == 0
    return true
  else
    return false
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What’s the difference between “.sort” and “.sort!”?

A

.sort - will sort a copy

.sort! - will sort current array, replacing old with the sorted version.

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

You have four books. Sort your books in descending order.

Display the result in the console.

A

books.sort! do |firstBook, secondBook|
secondBook <=> firstBook }

puts books

17
Q

Create a method, welcome, that puts “Welcome to Ruby!” After defining your method, call it.

A

def welcome
puts “Welcome to Ruby!”
end

welcome

18
Q

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

Add a block that multiplies each item by itself and puts the result to the console.

A

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

my_array.each do |num|
puts num * num
end

19
Q

Define a method to alphabetize an array (arr) or list in descending order (rev) if called upon.

A
def alphabetize(arr, rev = false)
  arr.sort!
    if rev == true
      arr.reverse!
    else
      return arr
    end
end
20
Q

How do you convert a string to a symbol?
How do you convert a symbol to a string?
How do you convert a string to an integer?
How do you convert something to a float?

A

.to_sym
.to_s
.to_i
.to_f

21
Q
olympic_trials = {
  Sally: 9.58,
  John: 9.69,
  Bob: 14.91
}

Grab all names/times that are 10.00 or better.

A

olympic_trials { |name, time| time < 10.00 }

22
Q

Write a method asking if a number is a multiple of 3 with the output resulting in true or false.

A

def multiple_of_three(n)
n % 3 == 0 ? “True” : “False”
end

23
Q

Write a loop that only puts the even values of my_array

A

my_array.each { |num| puts num unless num % 2 !=0 }

24
Q

What is the shortcut for .push?

A

&laquo_space;(shove)
alphabet &laquo_space;“x”

+ works on variables too

25
Q

Write a method, square, that takes a number as an argument and implicitly returns the square of that number.

A

def square(num)
num*num
end

26
Q

create a proc called cube that cubes a number

A

cube = Proc.new { |x| x ** 3 }