Control Flow Flashcards
true && true # => ___
true && false # => ___
false && true # => ___
false && false # => ___
true
false
false
false
true || true # => ___
true || false # => ___
false || true # => ___
false || false # => ___
true
true
true
false
false || false
true or false?
false
!true # => ____
!false # => ____
false
true
!true && !true
true or false?
false
!true && (!true || 100 != 5**2)
true or false?
false
true || !(true || false)
true or false?
true
Set the output to write “Ruby!” five times.
5.times { puts “Ruby!” }
Write an array of numbers 1-5. Note the brackets.
numbers = [1, 2, 3, 4, 5]
- Write a hash “profile” with your name, city, and state.
- Call on the city within that profile.
- Create a new hash called “favorites”.
- Add a value pair where “baseball” = “Twins”.
profile = { "name" => "Spencer" "city" => "Minneapolis" "state" => "MN" }
puts profile[“city”]
favorites = Hash.new favorites["baseball"] = "Twins"
example = [“Car”, “Boar”, 45, 9.9, true]
Access “45” in the array.
puts example[2]
- Create an array “team” of these players and their number:
Towns = 32
Russell = 0
Beasley = 5 - Call on the array to form a statement of what each number every player wears.
team = { "Towns" => "32", "Russell" => "0", "Beasley" => "5", }
team.each do |player, number|
puts “#{player} wears #{number}.”
end
Define a greeter method that takes a single string parameter, name, and returns a string greeting that person.
def greeter(name) return "Hello, #{name}!" end
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.
def by_three?(num) if num % 3 == 0 return true else return false end end
What’s the difference between “.sort” and “.sort!”?
.sort - will sort a copy
.sort! - will sort current array, replacing old with the sorted version.
You have four books. Sort your books in descending order.
Display the result in the console.
books.sort! do |firstBook, secondBook|
secondBook <=> firstBook }
puts books
Create a method, welcome, that puts “Welcome to Ruby!” After defining your method, call it.
def welcome
puts “Welcome to Ruby!”
end
welcome
my_array = [1, 2, 3, 4, 5]
Add a block that multiplies each item by itself and puts the result to the console.
my_array = [1, 2, 3, 4, 5]
my_array.each do |num|
puts num * num
end
Define a method to alphabetize an array (arr) or list in descending order (rev) if called upon.
def alphabetize(arr, rev = false) arr.sort! if rev == true arr.reverse! else return arr end end
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?
.to_sym
.to_s
.to_i
.to_f
olympic_trials = { Sally: 9.58, John: 9.69, Bob: 14.91 }
Grab all names/times that are 10.00 or better.
olympic_trials { |name, time| time < 10.00 }
Write a method asking if a number is a multiple of 3 with the output resulting in true or false.
def multiple_of_three(n)
n % 3 == 0 ? “True” : “False”
end
Write a loop that only puts the even values of my_array
my_array.each { |num| puts num unless num % 2 !=0 }
What is the shortcut for .push?
«_space;(shove)
alphabet «_space;“x”
+ works on variables too
Write a method, square, that takes a number as an argument and implicitly returns the square of that number.
def square(num)
num*num
end
create a proc called cube that cubes a number
cube = Proc.new { |x| x ** 3 }