Skip to main content
Caleb-Mitchell

RB109: Practice Examples - Written

·7 mins
##---------------------------------------------------------- Unorganized

pet = "dog"

loop do |pet|
  pet = "cat"
  break
end

puts pet

## 2

pet = "dog"

loop do |animal|
  pet = "cat"
  break
end

puts pet

## 3

def test(b)
  b.map {|letter| puts "I like the letter: #{letter}"}
end

a = ['a', 'b', 'c']

puts test(a)

## 4

a = "hello"
b = "welcome"
c = "goodbye"
d = a
a = c
b = a
c = b

## 5

def test
  puts "Preparing assessment"
end

var = test

if var
  puts "written assessment"
else
  puts "interview"
end

## 6

a = 'a'
b = []
3.times do
  a << '!'
  b << a
end

p b

## 7

str = 'abc'
characters = ((0...str.size).each_with_object([]) do |i, arr|
  arr +=  [str[i]]
end)

p characters

## 8

def fix(value)
  value.concat('!')
  value = value.upcase
  value << 'xyz'
end
s = 'hello'
t = fix(s)

p t

## 9

a = 'hi'
b = a
c = b
a = b.upcase
c = b.upcase!

p a
p b
p c

p a.object_id
p b.object_id
p c.object_id

## 10

def change_word(str)
  str += '!!!'
  str.upcase!
end

word = 'hello'
change_word(word)
p word

# 11

[[1, 2], [3, 4]].map { |arr| puts arr[0] }

# 12

a = 1
b = 2

if a == true
  puts "Hallo!"
elsif b = false
  puts "Goodbye!"
else
  puts "No greeting for you!"
end

# 13

a = 1
b = 2

if a == true
  puts "Hallo!"
elsif b = 3
  puts "Goodbye!"
else
  puts "No greeting for you!"
end

# 14

x = 'Sally'

[1, 2, 3].each do |x|
  x = 'Joe'
end

p x

##---------------------------------------------------------- Local Variable Scope

a = Hello
b = a
a = Goodbye
puts a
puts b

## 2

a = 4

loop do
  a = 5
  b = 3

  break
end

puts a
puts b

## 3

a = 4
b = 2

loop do
  c = 3
  a = c
  break
end

puts a
puts b

## 4

def example(str)
  i = 3
  loop do
    puts str
    i -= 1
    break if i == 0
  end
end

example('hello')

## 5

def greetings(str)
  puts str
  puts "Goodbye"
end

word = "Hello"

greetings(word)

## 6

arr = [1, 2, 3, 4]
counter = 0
sum = 0

loop do
  sum += arr[counter]
  counter += 1
  break if counter == arr.size
end 

puts "Your total is #{sum}"

## 7

a = 'Bob'

5.times do |x|
  a = 'Bill'
end

p a

##---------------------------------------------------------- Variable Shadowing

a = 4
b = 2

2.times do |a|
  a = 5
  puts a
end

puts a
puts b

## 2

n = 10

1.times do |n|
  n = 11
end

puts n

## 3

animal = "dog"

loop do |animal|
  animal = "cat"
  break
end

puts animal

##---------------------------------------------------------- Object Passing / Variables as Pointers

a = "hi there"
b = a
a = "not here"

## 2

a = "hi there"
b = a
a << ", Bob"

## 3

a = [1, 2, 3, 3]
b = a
c = a.uniq

## 4

def test(b)
  b.map {|letter| "I like the letter: #{letter}"}
end

a = ['a', 'b', 'c']
test(a)

## 5

a = 5.2
b = 7.3

a = b

b += 1.1

## 6

def test(str)
  str  += '!'
  str.downcase!
end

test_str = 'Written Assessment'
test(test_str)

puts test_str

## 7

def plus(x, y)
  x = x + y
end

a = 3
b = plus(a, 2)

puts a
puts b

## 8

def increment(x)
  x << 'b'
end

y = 'a'
increment(y) 

puts y

## 9

def change_name(name)
  name = 'bob'
end

name = 'jim'
change_name(name)
puts name 

## 10

def cap(str)
  str.capitalize!
end

name = "jim"
cap(name)
puts name 

## 11

a = [1, 3]
b = [2]
arr = [a, b]
arr

a[1] = 5
arr

## 12

arr1 = ["a", "b", "c"]
arr2 = arr1.dup
arr2.map! do |char|
  char.upcase
end

puts arr1 
puts arr2

##---------------------------------------------------------- Object Mutability / Mutating Methods

def fix(value)
  value.upcase!
  value.concat('!')
  value
end

s = 'hello'
t = fix(s)

p s
p t

## 2

def fix(value)
  value = value.upcase
  value.concat('!')
end

s = 'hello'
t = fix(s)

p s
p t

## 3

def fix(value)
  value << 'xyz'
  value = value.upcase
  value.concat('!')
end

s = 'hello'
t = fix(s)

p s
p t

## 4

def fix(value)
  value = value.upcase!
  value.concat('!')
end

s = 'hello'
t = fix(s)

p s
p t

## 5 

def fix(value)
  value[1] = 'x'
  value 
 end

s = 'abc'
t = fix(s)

p s
p t

## 6

def a_method(string)
  string << ' world'
end

a = 'hello'
a_method(a)

p a

## 7

num = 3

num = 2 * num

p num

## 8

a = %w(a b c)
a[1] = '-'
p a

## 9

def add_name(arr, name)
  arr = arr << name
end

names = ['bob', 'kim']
add_name(names, 'jim')
puts names

## 10

def add_name(arr, name)
  arr = arr + [name]
end

names = ['bob', 'kim']
add_name(names, 'jim')
puts names

##---------------------------------------------------------- Each, Map, Select

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

new_arr = array.select do |num|
   puts num if num.odd?
end

p new_arr

## 2

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

new_arr = arr.select { |n| n.odd? }

p new_arr

## 3

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

new_array = arr.select do |n| 
  n + 1
end

p new_array

## 4

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

new_array = arr.select do |n| 
  n + 1
  puts n
end

p new_array

## 5

words = %w(jump trip laugh run talk)

new_array = words.map do |word|
  word.start_with?("t")
end

p new_array

## 6

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

p arr.each { |n| puts n }

## 7

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

incremented = arr.map do |n| 
            n + 1
            end

p incremented

## 8

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

new_array = arr.map do |n| 
  n > 1
end

p new_array

## 9

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

new_array = arr.map do |n| 
  n > 1
  puts n
end

p new_array

## 10

a = "hello"

new_arr = [1, 2, 3].map { |num| a }

p new_arr

## 11

[1, 2, 3].each do |num|
  puts num
end

##---------------------------------------------------------- Other Collection Methods

p [1, 2, 3].any? do |num|
  num > 2
end

## 2

hash = { a: "ant", b: "bear", c: "cat" }.any? do |key, value|
  value.size > 4
end

p hash

## 3

new_arr = [1, 2, 3].all? do |num|
  num > 2
end

p new_arr

## 4

{ a: "ant", b: "bear", c: "cat" }.all? do |key, value|
  value.length >= 3
end

## 5

[1, 2, 3].each_with_index do |num, index|
  puts "The index of #{num} is #{index}."
end

## 6

hash =  { a: "ant", b: "bear", c: "cat" }

new_hash = hash.each_with_object([]) do |pair, array|
  array << pair.last
end

p hash
p new_hash

## 7

hash = { a: "ant", b: "bear", c: "cat" }

new_hash = hash.each_with_object({}) do |(key, value), hash|
  hash[value] = key
end

p hash
p new_hash

## 8

odd, even = [1, 2, 3].partition do |num|
  num.odd?
end

p odd 
p even

##---------------------------------------------------------- Truthiness

a = "Hello"

if a
  puts "Hello is truthy"
else
  puts "Hello is falsey"
end

## 2

def test
  puts "written assessment"
end

var = test

if var
  puts "written assessment"
else
  puts "interview"
end

##---------------------------------------------------------- Precedence

array = [1, 2, 3, 4, 5, 6]

p array.select do |num| 
  puts num if num.odd?
end

p array.select { |num| puts num if num.odd? }

p (array.select) { |num| puts num if num.odd? }