Possible Duplicate:
How do I use the conditional operator (? :) in Ruby?
I'm teaching myself Ruby and going through the RubyMonk exercises. I came across this code, which confuses me:
def calculate(*arguments)
options = arguments[-1].is_a?(Hash) ? arguments.pop : {}
options[:add] = true if options.empty?
return add(*arguments) if options[:add]
return subtract(*arguments) if options[:subtract]
end
Note that add and subtract are existing functions that add/subtract its arguments, which could vary in length.
Calculate is supposed to work like this
calculate(1,2,3,4,5,add: true) => 10
calculate(10,3,4, subtract: true) => 3
My question is can someone explain what is happening in the line:
options = arguments[-1].is_a?(Hash) ? arguments.pop : {}
Namely, what exactly does a standalone question mark do? Also, what does the colon do?
Thanks a lot for your help!