What is the Value?

1. What is the value of greeting?

def upper_case(str)
str.upcase!
end
greeting = "hello"
upper_case(greeting)

When greeting is passed into upper_case a local method variable called str is initialized that references the SAME object as greeting(greeting & str are aliases for the same object). The value of greeting is therefore "HELLO" .

2. What is the value of greeting2?

def upper_case(str)
str.upcase!
end
greeting = "hello"
greeting2 = upper_case(greeting)

greeting2 references the same object as greeting which has a value of “HELLO”.

3. What is the value of greeting?

def upper_case(str)
str = "hello"
str.upcase!
end
greeting = "hello"
upper_case(greeting)

When greeting is passed into upper_case , the local method variable str is initialized and references the same object. On line 2 however, str is reassigned to a new object (both objects have the value of “hello”). This means any changes made to the object str references will not modify the object that greeting references. greeting therefore has a value of ‘hello’.

4. What is the value of greeting2?

def upper_case(str)
str = "hello"
str.upcase!
end
greeting = "hello"
greeting2 = upper_case(greeting)

The method upper_case returns the object that str references. greeting2 is initialized to reference the return of upper_case. greeting2 = "HELLO"

5. What is the value of greetings?

def yell!(message)
message << '!'
end
greetings = %w(hi hello howdy)
my_greeting = greetings[0]
yell!(my_greeting)

greetings is initialized as an Array object with three objects. my_greeting is initialized as a reference to the first element in greetings , a String object with the value "hi" . Therefore greeting will have the value["hi!", "hello", "howdy"] .