101 Assessment study cards Flashcards

1
Q

What is the output of the following?

str = “a string”

def str
– “a method”
end

p str

A

p str
=> ‘a string’

this is because the variable initialized inside of the method is not available outside the method. The str variable outside of the method is what is printed out.

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

Does this method have a problem?array = [1, 2, 3]

array.each do |element|
a = element
end

puts a

A

Yes a is defined inside the block without being assigned a value outside first. Throws an error message.

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