You are on page 1of 32

Suggested response format (based on feedback from other students & Srdjan’s blog post):

● What does the code output? What are the return values?
● Answer the why behind the output/return:
○ Focus only on the lines of code that deliver the output and return values.
● Summarize what the problem demonstrates and why: ​This problem demonstrates
local variable scope/etc…
○ This can be at the beginning or end of your answer - personal preference.

Using Markdown: use `backticks` (Markdown formatting) to highlight variable names,


methods, and lines you are referring to:​ On `line 1` we initialize the local variable…

Always aim to answer: What does the following code


output and return? Why? What concept does it
demonstrate?
Additional Practice Problems:
1. Collection Methods from Lesson 4
2. Ruby Basics: Variable Scope
3. Ruby Basics: Return

Local Variable Scope

Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​'Hello
b = a
a = '​Goodbye​'

Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​4

loop ​do
a = ​5
b = ​3
​ reak
b
end

puts a
puts b

Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​4
b = 2​

loop ​do
c = ​3
a = c
​break
end

puts a
puts b

Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​example​(str)
i = ​3
loop ​do
puts str
i -= ​1
​break​ ​if​ i == ​0
​end
end

example(​'hello'​)
Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​greetings​(str)
puts str
puts ​"Goodbye"
end

word = ​"Hello"

greetings(word)
Problem link

Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
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}​"

Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​'Bob'

5​.times ​do​ |x|


a = ​'Bill'
end
p a

Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
animal = ​"dog"

loop ​do​ |_|


animal = ​"cat"
var = ​"ball"
​break
end

puts animal
puts var

Variable Shadowing

Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​4
b = 2​

2​.times ​do​ |a|


a = ​5
puts a
end

puts a
puts b

Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
n = ​10

1​.times ​do​ |n|


n = ​11
end

puts n

Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
animal = ​"dog"

loop ​do​ |animal|


animal = ​"cat"
​break
end

puts animal

Object Passing/Variables As Pointers

Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​"hi there"
b = a
a = ​"not here"
What are a and b?

Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​"hi there"
b = a
a << ​", Bob"
What are a and b?

Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = [​1​, ​2​, ​3​, ​3​]
b = a
c = a.uniq
What are a, b, and c? What if the last line was `c = a.uniq!`?

Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​test​(b)
b.map {|letter| ​"I like the letter: ​#{letter}​"​}
end

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


test(a)
What is `a`? What if we called `map!` instead of `map`?

Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​5.2
b = 7​ .3

a = b

b += ​1.1
What is `a` and `b`? Why?

Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​test​(str)
str += ​'!'
str.downcase!
end

test_str = ​'Written Assessment"


test(test_str)

puts test_str
Link​ to explanation of examples below

Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
​ lus​(x, y)
def​ p
x = x + y
end

a = ​3
b = plus(a, ​2​)

puts a
puts b

Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
​ ncrement​(x)
def​ i
x << ​'b'
end

y = ​'a'
increment(y)

puts y

Example 9
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​change_name​(name)
name = ​'bob'​ ​# does this reassignment change the object outside the
method?
end

name = ​'jim'
change_name(name)
puts name

Example 10
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​cap​(str)
str.capitalize! ​# does this affect the object outside the method?
end

name = ​"jim"
cap(name)
puts name

Example 11
What is `arr`? Why? What concept does it demonstrate?
a = [​1​, ​3​]
b = [​2​]
arr = [a, b]
arr

a[​1​] = ​5
arr

Example 12
Link to example
arr1 = [​"a"​, ​"b"​, ​"c"​]
arr2 = arr1.dup
arr2.map! ​do​ |char|
char.upcase
end

puts arr1
puts arr2

Object Mutability/Mutating Methods


Here’s the article​ with some of the below examples
Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​fix​(value)
value.upcase!
value.concat(​'!'​)
value
end

s = ​'hello'
t = fix(s)
What values do `s` and `t` have? Why?

Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​fix​(value)
value = value.upcase
value.concat(​'!'​)
end

s = ​'hello'
t = fix(s)
What values do `s` and `t` have? Why?

Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​fix​(value)
value << ​'xyz'
value = value.upcase
value.concat(​'!'​)
end

s = ​'hello'
t = fix(s)
What values do `s` and `t` have? Why?
Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​fix​(value)
value = value.upcase!
value.concat(​'!'​)
end

s = ​'hello'
t = fix(s)
What values do `s` and `t` have? Why?

Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​fix​(value)
value[​1​] = ​'x'
value
end

s = ​'abc'
t = fix(s)
What values do `s` and `t` have? Why?

Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​a_method​(string)
string << ​' world'
end

a = ​'hello'
a_method(a)

p a

Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
num = ​3

num = ​2​ * num

Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​%w(a b c)
a[​1​] = ​'-'
p a

Example 9
Link to page with #9 & #10
def​ ​add_name​(arr, name)
arr = arr + [name]
end

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

Example 10

def​ ​add_name​(arr, name)


arr = arr << name
end

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

Each, Map, Select

Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
array = [​1​, ​2​, ​3​, ​4​, ​5​]
array.select ​do​ |num|
puts num ​if​ num.odd?
end

Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [​1​, ​2​, ​3​, ​4​, ​5​, ​6​, ​7​, ​8​, ​9​, ​10​]

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

Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [​1​, ​2​, ​3​, ​4​, ​5​, ​6​, ​7​, ​8​, ​9​, ​10​]

new_array = arr.select ​do​ |n|


n + ​1
end
p new_array

Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
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

Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
words = ​%w(jump trip laugh run talk)

words.map ​do​ |word|


word.start_with?(​"t"​)
end

Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [​1​, ​2​, ​3​, ​4​, ​5​, ​6​, ​7​, ​8​, ​9​, ​10​]

arr.each { |n| puts n }

Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [​1​, ​2​, ​3​, ​4​, ​5​, ​6​, ​7​, ​8​, ​9​, ​10​]

incremented = arr.map ​do​ |n|


n + ​1
​end
p incremented

Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [​1​, ​2​, ​3​, ​4​, ​5​, ​6​, ​7​, ​8​, ​9​, ​10​]

new_array = arr.map ​do​ |n|


n > ​1
end
p new_array

Example 9
What does the following code return? What does it output? Why? What concept does it
demonstrate?
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

Example 10
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​"hello"

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

Example 11
What does the following code return? What does it output? Why? What concept does it
demonstrate?
[​1​, ​2​, 3
​ ​].each ​do​ |num|
puts num
end

Other Collection Methods


Link to all examples below

Example 1

[​1​, ​2​, ​3​].any? ​do​ |num|


num > ​2
end

Example 2

{ ​a:​ ​"ant"​, ​b:​ "


​ bear"​, ​c:​ ​"cat"​ }.any? ​do​ |key, value|
value.size > 4 ​
end

Example 3

[​1​, ​2​, ​3​].all? ​do​ |num|


num > ​2
end
Example 4

{ ​a:​ ​"ant"​, ​b:​ "


​ bear"​, ​c:​ ​"cat"​ }.all? ​do​ |key, value|
value.length >= ​3
end

Example 5

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

Example 6

{ ​a:​ ​"ant"​, ​b:​ ​"bear"​, ​c:​ ​"cat"​ }.each_with_object([]) ​do​ |pair, array|
array << pair.last
end

Example 7

{ ​a:​ ​"ant"​, ​b:​ ​"bear"​, ​c:​ ​"cat"​ }.each_with_object({}) ​do​ |(key, value),
hash|
hash[value] = key
end

Example 8

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


num.odd?
end

p odd
p even

Truthiness

Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = ​"Hello"
if​ a
​ Hello is truthy"
puts "
else
puts "​ Hello is falsey"
end

Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def​ ​test
puts ​"written assessment"
end

var = test

if​ var
​ written assessment"
puts "
else
puts "​ interview"
end
Shaun’s work below

a = ​'Hello
b = a
a = '​Goodbye​'

On `line 1` we are initializing the local variable `a` and assigning it to


the string object ‘Hello’

On `line 2` we are initializing the local variable ‘b’ and assigning it to


the same string object that `a` is pointing to.

On `line 3` we are reassigning `a` to the string object ‘Goodbye’

This example demonstrates variable reassignment. (should we say more than


this?)

a = ​4

loop ​do
a = ​5
b = ​3
​break
end

puts a
puts b

On `line 1` we are initializing the local variable `a` and assigning it to


the integer object 4.

On `line 2` we are calling the `loop` method and passing in the `do..end`
block as an argument.

On `line 3` we are reassigning the local variable `a` to the integer object
5.

On `line 4` we are initializing the local variable `b` and assigning it to


the integer object 3.

On `line 5` we are inserting a break condition (is this right?)


`Line 7` outputs the integer object 5 and returns nil.
`Line 8` will return an error. ​This is an example of variable scope;
specifically, that variables initialized within a block are NOT
accessible outside of that block, in the outer scope.

Because the local variable `b` was initialized in the inner scope of
the ‘do..end’ block that we passed to the `loop method` as an
argument, it is not available in the other scope.

New work starts here

a = ​"hello"

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

This will output nothing and return an array of string objects: [‘hello’, ‘hello’, ‘hello’]

On `line 1` we are initializing the local variable `a` and assigning it the string object `hello`.

On `line 3` we are calling the `map` method on the array object [1, 2, 3] and passing in the `{}`
block as an argument.

The `map` method iterates through the array object, passing each element to the block and then
runs the block. It then takes the return value of the block and moves it to a new array. And when
it finishes every iteration, it returns that new array that now contains the elements that were
passed into it, which is this case was [‘hello’, ‘hello’, ‘hello’]

Because `a` in the block evaluates to `hello` each time `map` runs the block, an array containing
the string object ‘hello’ is returned.

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

word = ​"Hello"

greetings(word)
This will output the string objects “Hello” and “Goodbye” and return `nil` (because puts always
returns `nil)

On `line 1-4` we are defining a method called `greetings` that takes one parameter `str`

on `line 2` we are calling the `puts` method and passing in `str` as an argument.

on `line 3` we are calling the `puts` method on the string object `Goodbye`

on `line 6` we are initializing the local variable `word` and assigning it the string object `Hello`

on `line 8` we are calling the `greetings` method and passing in the local variable `word` as an
argument. `Str` now points to the same String object that `word` points to `”Hello”`

On `line 2` `str` is now pointing to the same string object that `word` is pointing to. And so this
is what is output when we call the `puts` method on `str` on `line 2` when we invoke the
`greetings` method on `line 8` (this does not feel good enough, but I don’t know what else needs
to be explained here)

Version 2

# this will output the string objects `Hello` and `Goodbye` and returns `nil`

#On `line 6` we are initialzing the local variable `word` and assigning it to the string object
`Hello`

#On `lines 1-4` we are defining the `greetings` method that takes one parameter `str`

#On `line 8` we are invoking the `greetings` method and passing in the local variable `word` as
an argument. `word` and `str` are now pointing to the same object.

#On `line 2` we are calling the `puts` method and passing in `str` as an argument.

#On `line 3` we are calling the `puts` method and passing in the string object `Goodbye` as an
argument.
#This problem demonstrates the concept of local variable scope. A local variable that is
initialized outside of a method is accessible inside a method defined with a parameter, if the local
variable is passed into the method as an argument. The method can now access the string
`"Hello"` since it is passed in as an argument when the method is invoked.

More work starts here


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

This will output the integer objects 1, 2, 3 and return the array object that it was called on,

`[1, 2, 3]’

On `line 1` we are calling the `each` method on the array object with values 1, 2, 3 and passing in
the `do..end` block as an argument, with one parameter, `num`

On `line 2` we are calling the `puts` method and passing it `num` as an argument.

As the `each` method iterates over each of the elements in the array, it sends the element to the
block parameter `num` which is then passed to the `puts` method call as an argument and output
to the screen.

This problem demonstrates that the `each` method returns the original collection it was called on.

[​1​, ​2​, ​3​, ​4​].select { |num| num.odd? }

This will output nothing and return the array object `[1, 3]`

On `line 1` we are calling the `select` method on the array object with
integers 1, 2, 3, 4.

We are passing the `select` method the `{}` block as an argument, with one
parameter, `num`
We are then calling the `odd?` method on `num`.

As the `select` method iterates over each of the elements in the array, it
sends the element to the block parameter `num`, which we then pass to the
`odd?` method. Each iteration will return a boolean value.

Whenever the `odd` method evaluates to `true`, the element will be selected
and passed into a new array. One the `select` method has finished iterating
over the collection, it will return the new array, which, in this case, will
be `[1, 3]`

This problem demonstrates that the `select` method returns a new Array object
based on the truthiness of the block’s return value. `select` uses the return
value of the block to perform selection on each element.

[​5​, ​6​, ​7​].each ​do​ |num|


if num > ​5
puts num
​end
end

This code outputs the integer objects 6 and 7 and returns the array object
[5, 6, 7]

On `line 1` we call the `each` method on the array object with integers 5, 6,
7. We pass in the `do..end` block as an argument, with one parameter, `num`

As the `each` method iterates over each of the elements in the array, it passes the element to the
block parameter `num`. which is then passed to the `>` method on `line 2`.

Inside the conditional statement, we invoke the `#>` method on the local variable `num` and pass
in the integer `5` as an argument. If `num` is greater than `5` then the code on `line 3` will be
executed. In this case, the integers `6` and `7` are output to the screen.

If the element is greater than the argument passed to the `>` method, which is the integer object
5, that element is then passed to the `puts` method on `line 3` and output to the screen.

This problem demonstrates that the `each` method returns the original collection it was called on.
a = ​'Bob'

5​.times ​do​ |x|


a = ​'Bill'
end

p a

This outputs the string object “Bill” and returns the string object “Bill”

On `line 1` we are initializing the local variable `a` and assigning it the
string object ‘Bob’

On `line 3` we are calling the `times` method on the integer object 5 and
passing in the `do..end` block as an argument with one parameter `x`

On `line 4` we are reassigning the local variable `a` to the string object
`Bill`

On `line 7` we are outputting the object that the local variable `a` is
pointing to, which is `Bill`

This is an example of local variable scope. Specifically, variables


initialized in the outer scope are available and can be reassigned in an
inner scope.

animal = ​"dog"

loop ​do​ |_|


animal = ​"cat"
var = ​"ball"
​break
end

puts animal
puts var

`line 9` will output the string object `cat` and return `nil`
`line 10` will produce an error, undefined local variable or method `var` for
main Object.
On `line 1` we initialize the local variable `animal` and assign it the
string object `dog`

On `line 2` we call the `loop` method and pass it the `do..end` block as an
argument.

On `line 3` we are reassigning the local variable `animal` to the string


object `cat`

On `line 4` we are initializing the local variable `var` and assigning it the
string object `ball`

On `line 8` we are calling teh `puts` method and passing it the local
variable `animal` as an argument. Because `animal` was reassigned within the
block, it now points to the string object `cat`

`line 9` produces an error because the local variable `var` was initialized
inside of the `do..end` block and thus is not available outside of the block.

This is an example of variable scope; specifically, that variables initialized in the outer scope are
available in an inner scope.

This is also an example of variable scope; specifically, that variables initialized within a block
are NOT accessible outside of that block, in the outer scope.

n = ​10

1​.times ​do​ |n|


n = ​11
end

puts n

This will output the integer object 10 and return the integer object 1. It
returns 1 because the `times` method returns the integer object that it was
called on.

What I should have said was:

This will output the integer `10` and return `nil`.


On `line 1` we initialize the local variable `n` and assign it the integer
object 10.
On `line 2` we call the `times` method on the integer object 1 and pass in
the `do..end` block as an argument with one parameter `n`
On `line 3` we assign the integer object 11 to the parameter `n`.
On `line 6` we call the `puts` method and pass in the local variable `n` as
an argument. Because the local variable initialized on `line 2` and the block
parameter have the same name, variable shadowing occurs, and so the local
variable `n` is NOT reassigned within the block. If the block parameter was
named anything else, `n` would be reassigned on `line 3`

This is an example of variable shadowing. Variable shadowing happens when the block
parameter name is the same as a local variable which was initialized outside of the block. The
consequence is that it prevents access to variables of the same name initialized outside of the
block. It also prevents us from making changes to the outer-scoped variable.

animal = ​"dog"

loop ​do​ |animal|


animal = ​"cat"
​break
end

puts animal

This code outputs the string object `dog` and returns `nil`.

On `line 1` we are initializing the local variable `animal` and assigning it the string object `dog`

On `line 3` we are calling the `loop` method and passing in the `do..end` block as an argument
with one parameter `animal`

Because the block parameter and the local variable initialized on `line 1` have the same name,
the local variable will NOT be avaliable within the block.
So when we call the `puts` method on `line 8` and pass in the local variable `animal` as an
argument, it will output the string object `dog` and return `nil`. However, if the block parameter
was called anything else, `animal` would have been reassigned to the string object `cat`.

This is an example of variable shadowing. Variable shadowing happens when the block
parameter name is the same as a local variable which was initialized outside of the block. The
consequence is that it prevents access to variables of the same name initialized outside of the
block. It also prevents us from making changes to the outer-scoped variable.

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

On `line 1` we are initializing the local variable `a` and assigning it the string object `hi there`

On `line 2` we are initializing the local variable `b` and assigning it to the same object that `a` is
pointing to. `a` and `b` are now pointing to the same string object `hi there`

On `line 3` we are reassigning `a` to the string object `not here`

This is an example of variable reassignment, where we reassign the local variable `a` to point to
a new string object. The local variable `b` is still pointing to the original string object that it was
assigned to, `hi there`

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

On `line 1` we are initializing the local variable `a` and assigning it the string object `hi there`.

On `line 2` we are initializing the local variable `b` and assigning it to the same object that `a` is
pointing to. `a` and `b` are now pointing to the same string object `hi there`

On `line 3` we are calling the `<<` method on the string object that `a` is pointing to, and passing
in the string object `, Bob` as an argument.

The line of code `a << ", Bob"` ​mutated the caller​ and modified the existing string, which is also
pointed to by the variable b. This explains why in this code, b reflects the changes to a - they're
both pointing to the same thing.

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

s = ​'hello'
t = fix(s)

p s
p t

# `line 10` outputs the string object `helloxyz` and returns the same string object, `helloxyz'
# `line 11` outputs the string object `HELLOXYZ!` and returns the same string object,
`HELLOXYZ!'

#on `line 8` we are initializing a local variable `t` and assigning it to the return value of calling
the `fix` method and passing it the local variable `s` as an arugment.

#on `line 1` we define a method called `fix` that takes one parameter `value`
#on `line 2` we are calling the `<<` method on `value` and passing in the string object `xyz` as an
argument. Because the `<<` method mutates the caller, the local variable `s` and `value` are now
pointing to the same string object 'helloxyz'

#on `line 3` we are reassigning `value` to the return value of calling the `upcase` method on the
object that `value` is pointing to. `value` is now pointing to the string object `HELLOXYZ`
while the local variable `s` is still pointing to the string object `helloxyz`

#on `line 4` we are calling the `concat` method on `value` and passing in the string ojbect `!` as
an argument. `value` now points to the string ojbect `HELLOXYZ!`

On `line 4` we are calling the `concat` method on `value` and passing in the string object `!` as
an argument. `value`is still pointing to the same object, but it has been mutated and is now
`HELLOXYZ!`

#since eht return value of the `fix` method call is the string object `HELLOXYZ!' that is what `t`
is pointing to.
# the local variable `s` is still pointing to the string ojbect `helloxyz`
# this is an example of both reassignment, where we are reassigning `value` to a new string
object on `line 3` as well as an example of mutating the caller, which we do on `line 2` and `line
4`

More Practice Below

arr = [ "d", "a", "e", "c", "b" ]


arr.sort
p arr

This will output the array object `[ "d", "a", "e", "c", "b" ]` and return the same array object, `[
"d", "a", "e", "c", "b" ]`

On `line 1` we are initializing the local variable `arr` and assigning it the array object `[ "d", "a",
"e", "c", "b" ]’

On `line 2` we are calling the `sort` method on the object that `arr` is pointing to. Because the
`sort` method is non-mutative, the object that `arr` was assigned to on `line 1` has not changed.

And so when we call the `p` method and pass in `arr` as an argument on `line 3`, the original
array object that `arr` was assigned to on `line 1` is output and returned (because the `p` method
outputs and returns the object it was passed as an argument)

This is an example of...I’m not sure what to say here. This is an example of how a method call
can reassign an object, but because we did not assign the return of the method call to a variable,
no reassignment takes place?

Or

This code is demonstrating the impact of a non-mutating method on the caller.

def​ ​test
puts ​"written assessment"
end

var = test

if​ var
​ written assessment"
puts "
else
puts "​ interview"
end
This code outputs the string object `interview` and returns `nil`

On `line 1-3` we are defining the `test` method.

On `line 5` we are initializing the local variable `var` and assigning it the
return value of calling the `test` method.

The return value of calling the `test` method is `nil` because `puts` always
returns `nil`

On `line 7-11` we initialize a conditional `if/else` statement. If the first


branch of the conditional statement evaluates to `true`, the code on `line 8`
will be executed.

But because the return value of calling the `test` method is `nil`, the first
branch of the conditional statement evaluates to `false`, so the code on
`line 10` is executed.

This is why the string object `interview` is output and why `nil` is
returned.

This code demonstrates that `puts` always returns `nil`. It also demonstrates
that when you initialize a variable and point it to a method call, you are
assigning the return value of that method call to the variable, and not the
output of that method.

a = ​"Hello"

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

This code will output the string object `Hello is truthy` and return `nil`.

On `line 1` we are initializing the local variable `a` and assigning it the string object `Hello`.
On `line 2-6` we are initializing a conditional `if/else` statement. If the first branch of the
conditional statement is `truthy`, the code on `line 3` will be executed.

Because every value in Ruby evaluates to `true`, except for `false` and `nil`, this means that the
local variable `a` evaluates to `true`, and so the code on `line 3` is executed.

This code demonstrates that every value in Ruby evaluates to `true`, except for `false` and `nil`.

Another, perhaps better way to answer this question would simply be:

The local variable `a` evaluates to `true` in the conditional statement and
so the string object 'Hello is truthy' is output.

{ ​a:​ ​"ant"​, ​b:​ "


​ bear"​, ​c:​ ​"cat"​ }.any? ​do​ |key, value|
value.size > 4 ​
end

This code will output nothing and return `false`

On `line 1` we are calling the `any?` method on the hash object with key/value pairs `​{ ​a:
"ant"​, ​b:​ ​"bear"​, ​c:​ ​"cat"​ }`

We are passing in the `do..end` block as an argument, with 2 parameters `key`


and `value`
On `line 2` we are calling the `size` method on the local variable `value`
and passing in `> 4` as an argument.

Because none of the values in the hash have a `size` greater than `4`,
`false` is returned.

This code demonstrates how the `any?` method passes each element of the
collection to the given block and returns `true` if the block ever returns a
value other than `false` or `nil`
[​1​, ​2​, ​3​].all? ​do​ |num|
num > ​2
end

This code outputs nothing and returns `false`

On `line 1` we are calling the `all?` method on the array object with values `1, 2, 3` and passing
in the `do..end` block as an argument with one parameter `num`

On `line 2` we are calling the `>` method on the local variable `num` and passing in the integer
`2` as an argument.

The method `all` requires that all objects in a collection meet the criteria; in this case, that they
have a value greater than `2`. Since all objects do not have a value greater than `2`, `false` is
returned.

This code demonstrates how the `all` method works. Specifically, that the method passes each
element of the collection to the given block and returns `true` if the block never returns `false` or
`nil`.

{ ​a:​ ​"ant"​, ​b:​ ​"bear"​, ​c:​ ​"cat"​ }.each_with_object([]) ​do​ |pair, array|
array << pair.last
end

This code outputs nothing and returns the array object `[“ant”, “bear”, “cat”]

​ :​ ​"ant"​, ​b:
On `line 1` we are calling the `each_with_object` method on the hash object `​{ a
"bear"​, ​c:​ ​"cat"​ }` ​and passing in the `do..end` block as an argument with
two parameters `pair` and `array`

On `line 2 we are calling the `<<` method on the local variable `array` and
passing in `pair.last` as an argument.

This will insert the `value` of each `key/value` pair into the new array.
This code demonstrates how the `each_with_object` method works. Specifically,
that the method iterates the given block for each element with an arbitrary
object given, and returns the initially given object.

{ ​a:​ ​"ant"​, ​b:​ ​"bear"​, ​c:​ ​"cat"​ }.each_with_object({}) ​do​ |(key, value),
hash|
hash[value] = key
end

This code will output nothing and return the hash object `{ “ant” => :a, “bear” => :b, “cat” => :c }`

​ :​ ​"ant"​, ​b:
On `line 1` we are calling the `each_with_object` method on the hash object `​{ a
"bear"​, ​c:​ ​"cat"​ }` ​and passing in the `do..end` block as an argument with
three parameters `key`, `value` and `hash`.

Completely blanking here...don’t know how to answer past this :(

This code demonstrates that I don’t know how to explain this concept.

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


num.odd?
end

p odd
p even
`​line 5` will output the array object `[1, 3]` and return the same
object (because the method call `p` always outputs and returns the
object it was called on)

`line 6` will output the array object `[2]` and return the same
object.

On `line 1` we are initializing the local variables `odd` and even` and assigning them to the
return value of calling the `partition` method on the array object `[1, 2, 3]` and passing it the
`do..end` block as an argument with one parameter `num`

On `line 2` we are calling the `odd?` method on the local variable `num`

This is an example of how the `partition` method call returns two arrays, the first containing the
elements for which the block evaluates to true, the second containing the rest.

So the local variable `odd` will contain the elements for which the block evaluates to `true`, and
the local variable `even` will contain the rest.

aO=

You might also like