Method Quiz
2.5.round
is an example of ____.
2.5.round
invokes the round
method upon the receiver 2.5
.def what_am_i
"When you look into an abyss, the abyss also looks into you."
end
The above code snippet is an example of ____.
what_am_i
method but doesn't invoke it.What is the receiver of 2.5.round
?
2
round
2.5
2.5
is the receiver, the object upon which the method is called. 3
is the return value of 2.5.round
. round
is the method itself.What is the argument of 2.gcd(4)
?
2
gcd
4
2
and 4
are both arguments.4
is the argument. 2
is the receiver. gcd
is the method itself.What is the return value of 2.even?
?
true
false
2
even?
2
is the receiver, even?
is the method, and invoking the even?
method with the receiver 2
returns true
.def pick_me #A
"pretty please"
def the_perfect_definition(arg_one, arg_two) #B
"look no further than my name"
end
def seriously_valid(arg_one arg_two) #C
"I'm seriously"
end
def def(arg_one, arg_two) #D
"I'm DEFinitely valid"
end
Which of the above method definitions uses valid syntax?
end
. The third lacks a comma between arguments. The fourth uses a Ruby keyword (def
) as a method name, which is invalid.def return_this(arg)
arg
end
return_this("return me, please")
What is the return value of the above method invocation?
"return me, please"
nil
"arg"
return_this
method with the argument "return me, please"
. In the method body, arg
is therefore assigned to "return me, please"
upon invocation. The value of arg
is then implicitly returned.def pitter_putter(arg)
puts arg
end
pitter_putter("patter")
What is the return value of the above method invocation?
"patter"
nil
"arg"
puts arg
) is implicitly returned. Because the return value of puts is always nil
, the return value of pitter_putter("patter")
is itself nil
.def nihilist(arg)
return nil
arg
end
nihilist("Nietzsche")
What is the return value of the above method invocation?
"Nietzsche"
nil
"arg"
nil
. The method's subsequent code (arg
) is unreachable and is therefore never executed.def nihilist(arg)
return nil
arg
end
What code in the above method definition is unreachable?
arg
end
nil
return nil
def nihilist(arg)
return nil
arg
end
nihilist("Nietzsche").to_s
What does the above method chain return?
"Nietzsche"
nil
""
nihilist("Nietzsche")
is nil
. nil.to_s
is an empty string (""
).def cat
"Cat"
end
def dog
"Dog"
end
def catdog
cat + dog + " was cartoon about a conjoined felnine"
end
catdog
Does the above code snippet throw an error?
cat
and dog
are valid helper methods of catdog
.